aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/allwinner/aw_nmi.c
blob: d242726c29b9ecc7b8b1ef7360ae7eba664524c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*-
 * Copyright (c) 2016 Emmanuel Vadot <manu@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/proc.h>
#include <sys/rman.h>
#include <machine/bus.h>
#include <machine/intr.h>

#include <dev/fdt/fdt_intr.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include "pic_if.h"

#define	NMI_IRQ_CTRL_REG	0x0
#define	 NMI_IRQ_LOW_LEVEL	0x0
#define	 NMI_IRQ_LOW_EDGE	0x1
#define	 NMI_IRQ_HIGH_LEVEL	0x2
#define	 NMI_IRQ_HIGH_EDGE	0x3
#define	NMI_IRQ_PENDING_REG	0x4
#define	 NMI_IRQ_ACK		(1U << 0)
#define	A20_NMI_IRQ_ENABLE_REG	0x8
#define	A31_NMI_IRQ_ENABLE_REG	0x34
#define	 NMI_IRQ_ENABLE		(1U << 0)

#define	R_NMI_IRQ_CTRL_REG	0x0c
#define	R_NMI_IRQ_PENDING_REG	0x10
#define	R_NMI_IRQ_ENABLE_REG	0x40

#define	SC_NMI_READ(_sc, _reg)		bus_read_4(_sc->res[0], _reg)
#define	SC_NMI_WRITE(_sc, _reg, _val)	bus_write_4(_sc->res[0], _reg, _val)

static struct resource_spec aw_nmi_res_spec[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
	{ -1,			0,	0 }
};

struct aw_nmi_intr {
	struct intr_irqsrc	isrc;
	u_int			irq;
	enum intr_polarity	pol;
	enum intr_trigger	tri;
};

struct aw_nmi_reg_cfg {
	uint8_t			ctrl_reg;
	uint8_t			pending_reg;
	uint8_t			enable_reg;
};

struct aw_nmi_softc {
	device_t		dev;
	struct resource *	res[2];
	void *			intrcookie;
	struct aw_nmi_intr	intr;
	struct aw_nmi_reg_cfg *	cfg;
};

static struct aw_nmi_reg_cfg a20_nmi_cfg = {
	.ctrl_reg =	NMI_IRQ_CTRL_REG,
	.pending_reg =	NMI_IRQ_PENDING_REG,
	.enable_reg =	A20_NMI_IRQ_ENABLE_REG,
};

static struct aw_nmi_reg_cfg a31_nmi_cfg = {
	.ctrl_reg =	NMI_IRQ_CTRL_REG,
	.pending_reg =	NMI_IRQ_PENDING_REG,
	.enable_reg =	A31_NMI_IRQ_ENABLE_REG,
};

static struct aw_nmi_reg_cfg a83t_r_nmi_cfg = {
	.ctrl_reg =	R_NMI_IRQ_CTRL_REG,
	.pending_reg =	R_NMI_IRQ_PENDING_REG,
	.enable_reg =	R_NMI_IRQ_ENABLE_REG,
};

static struct ofw_compat_data compat_data[] = {
	{"allwinner,sun7i-a20-sc-nmi", (uintptr_t)&a20_nmi_cfg},
	{"allwinner,sun6i-a31-sc-nmi", (uintptr_t)&a31_nmi_cfg},
	{"allwinner,sun6i-a31-r-intc", (uintptr_t)&a83t_r_nmi_cfg},
	{"allwinner,sun8i-a83t-r-intc", (uintptr_t)&a83t_r_nmi_cfg},
	{NULL, 0},
};

static int
aw_nmi_intr(void *arg)
{
	struct aw_nmi_softc *sc;

	sc = arg;

	if (SC_NMI_READ(sc, sc->cfg->pending_reg) == 0) {
		device_printf(sc->dev, "Spurious interrupt\n");
		return (FILTER_HANDLED);
	}

	if (intr_isrc_dispatch(&sc->intr.isrc, curthread->td_intr_frame) != 0) {
		SC_NMI_WRITE(sc, sc->cfg->enable_reg, ~NMI_IRQ_ENABLE);
		device_printf(sc->dev, "Stray interrupt, NMI disabled\n");
	}

	return (FILTER_HANDLED);
}

static void
aw_nmi_enable_intr(device_t dev, struct intr_irqsrc *isrc)
{
	struct aw_nmi_softc *sc;

	sc = device_get_softc(dev);

	SC_NMI_WRITE(sc, sc->cfg->enable_reg, NMI_IRQ_ENABLE);
}

static void
aw_nmi_disable_intr(device_t dev, struct intr_irqsrc *isrc)
{
	struct aw_nmi_softc *sc;

	sc = device_get_softc(dev);

	SC_NMI_WRITE(sc, sc->cfg->enable_reg, ~NMI_IRQ_ENABLE);
}

static int
aw_nmi_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
    enum intr_polarity *polp, enum intr_trigger *trigp)
{
	u_int irq, tripol;
	enum intr_polarity pol;
	enum intr_trigger trig;

	if (ncells != 2) {
		device_printf(dev, "Invalid #interrupt-cells\n");
		return (EINVAL);
	}

	irq = cells[0];
	if (irq != 0) {
		device_printf(dev, "Controller only support irq 0\n");
		return (EINVAL);
	}

	tripol = cells[1];

	switch (tripol) {
	case FDT_INTR_EDGE_RISING:
		trig = INTR_TRIGGER_EDGE;
		pol  = INTR_POLARITY_HIGH;
		break;
	case FDT_INTR_EDGE_FALLING:
		trig = INTR_TRIGGER_EDGE;
		pol  = INTR_POLARITY_LOW;
		break;
	case FDT_INTR_LEVEL_HIGH:
		trig = INTR_TRIGGER_LEVEL;
		pol  = INTR_POLARITY_HIGH;
		break;
	case FDT_INTR_LEVEL_LOW:
		trig = INTR_TRIGGER_LEVEL;
		pol  = INTR_POLARITY_LOW;
		break;
	default:
		device_printf(dev, "unsupported trigger/polarity 0x%2x\n",
		    tripol);
		return (ENOTSUP);
	}

	*irqp = irq;
	if (polp != NULL)
		*polp = pol;
	if (trigp != NULL)
		*trigp = trig;
	return (0);
}

static int
aw_nmi_map_intr(device_t dev, struct intr_map_data *data,
    struct intr_irqsrc **isrcp)
{
	struct intr_map_data_fdt *daf;
	struct aw_nmi_softc *sc;
	int error;
	u_int irq;

	if (data->type != INTR_MAP_DATA_FDT)
		return (ENOTSUP);

	sc = device_get_softc(dev);
	daf = (struct intr_map_data_fdt *)data;

	error = aw_nmi_map_fdt(dev, daf->ncells, daf->cells, &irq, NULL, NULL);
	if (error == 0)
		*isrcp = &sc->intr.isrc;

	return (error);
}

static int
aw_nmi_setup_intr(device_t dev, struct intr_irqsrc *isrc,
    struct resource *res, struct intr_map_data *data)
{
	struct intr_map_data_fdt *daf;
	struct aw_nmi_softc *sc;
	struct aw_nmi_intr *nmi_intr;
	int error, icfg;
	u_int irq;
	enum intr_trigger trig;
	enum intr_polarity pol;

	/* Get config for interrupt. */
	if (data == NULL || data->type != INTR_MAP_DATA_FDT)
		return (ENOTSUP);

	sc = device_get_softc(dev);
	nmi_intr = (struct aw_nmi_intr *)isrc;
	daf = (struct intr_map_data_fdt *)data;

	error = aw_nmi_map_fdt(dev, daf->ncells, daf->cells, &irq, &pol, &trig);
	if (error != 0)
		return (error);
	if (nmi_intr->irq != irq)
		return (EINVAL);

	/* Compare config if this is not first setup. */
	if (isrc->isrc_handlers != 0) {
		if (pol != nmi_intr->pol || trig != nmi_intr->tri)
			return (EINVAL);
		else
			return (0);
	}

	nmi_intr->pol = pol;
	nmi_intr->tri = trig;

	if (trig == INTR_TRIGGER_LEVEL) {
		if (pol == INTR_POLARITY_LOW)
			icfg = NMI_IRQ_LOW_LEVEL;
		else
			icfg = NMI_IRQ_HIGH_LEVEL;
	} else {
		if (pol == INTR_POLARITY_HIGH)
			icfg = NMI_IRQ_HIGH_EDGE;
		else
			icfg = NMI_IRQ_LOW_EDGE;
	}

	SC_NMI_WRITE(sc, sc->cfg->ctrl_reg, icfg);

	return (0);
}

static int
aw_nmi_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
    struct resource *res, struct intr_map_data *data)
{
	struct aw_nmi_softc *sc;

	sc = device_get_softc(dev);

	if (isrc->isrc_handlers == 0) {
		sc->intr.pol = INTR_POLARITY_CONFORM;
		sc->intr.tri = INTR_TRIGGER_CONFORM;

		SC_NMI_WRITE(sc, sc->cfg->enable_reg, ~NMI_IRQ_ENABLE);
	}

	return (0);
}

static void
aw_nmi_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
{
	struct aw_nmi_softc *sc;

	sc = device_get_softc(dev);
	aw_nmi_disable_intr(dev, isrc);
	SC_NMI_WRITE(sc, sc->cfg->pending_reg, NMI_IRQ_ACK);
}

static void
aw_nmi_post_ithread(device_t dev, struct intr_irqsrc *isrc)
{

	arm_irq_memory_barrier(0);
	aw_nmi_enable_intr(dev, isrc);
}

static void
aw_nmi_post_filter(device_t dev, struct intr_irqsrc *isrc)
{
	struct aw_nmi_softc *sc;

	sc = device_get_softc(dev);

	arm_irq_memory_barrier(0);
	SC_NMI_WRITE(sc, sc->cfg->pending_reg, NMI_IRQ_ACK);
}

static int
aw_nmi_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
		return (ENXIO);
	device_set_desc(dev, "Allwinner NMI Controller");
	return (BUS_PROBE_DEFAULT);
}

static int
aw_nmi_attach(device_t dev)
{
	struct aw_nmi_softc *sc;
	phandle_t xref;

	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->cfg = (struct aw_nmi_reg_cfg *)
	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;

	if (bus_alloc_resources(dev, aw_nmi_res_spec, sc->res) != 0) {
		device_printf(dev, "can't allocate device resources\n");
		return (ENXIO);
	}
	if ((bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC,
	    aw_nmi_intr, NULL, sc, &sc->intrcookie))) {
		device_printf(dev, "unable to register interrupt handler\n");
		bus_release_resources(dev, aw_nmi_res_spec, sc->res);
		return (ENXIO);
	}

	/* Disable and clear interrupts */
	SC_NMI_WRITE(sc, sc->cfg->enable_reg, ~NMI_IRQ_ENABLE);
	SC_NMI_WRITE(sc, sc->cfg->pending_reg, NMI_IRQ_ACK);

	xref = OF_xref_from_node(ofw_bus_get_node(dev));
	/* Register our isrc */
	sc->intr.irq = 0;
	sc->intr.pol = INTR_POLARITY_CONFORM;
	sc->intr.tri = INTR_TRIGGER_CONFORM;
	if (intr_isrc_register(&sc->intr.isrc, sc->dev, 0, "%s,%u",
	      device_get_nameunit(sc->dev), sc->intr.irq) != 0)
		goto error;

	if (intr_pic_register(dev, (intptr_t)xref) == NULL) {
		device_printf(dev, "could not register pic\n");
		goto error;
	}
	return (0);

error:
	bus_teardown_intr(dev, sc->res[1], sc->intrcookie);
	bus_release_resources(dev, aw_nmi_res_spec, sc->res);
	return (ENXIO);
}

static device_method_t aw_nmi_methods[] = {
	DEVMETHOD(device_probe,		aw_nmi_probe),
	DEVMETHOD(device_attach,	aw_nmi_attach),

	/* Interrupt controller interface */
	DEVMETHOD(pic_disable_intr,	aw_nmi_disable_intr),
	DEVMETHOD(pic_enable_intr,	aw_nmi_enable_intr),
	DEVMETHOD(pic_map_intr,		aw_nmi_map_intr),
	DEVMETHOD(pic_setup_intr,	aw_nmi_setup_intr),
	DEVMETHOD(pic_teardown_intr,	aw_nmi_teardown_intr),
	DEVMETHOD(pic_post_filter,	aw_nmi_post_filter),
	DEVMETHOD(pic_post_ithread,	aw_nmi_post_ithread),
	DEVMETHOD(pic_pre_ithread,	aw_nmi_pre_ithread),

	{0, 0},
};

static driver_t aw_nmi_driver = {
	"aw_nmi",
	aw_nmi_methods,
	sizeof(struct aw_nmi_softc),
};

static devclass_t aw_nmi_devclass;

EARLY_DRIVER_MODULE(aw_nmi, simplebus, aw_nmi_driver,
    aw_nmi_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);