xref: /freebsd/sys/arm/nvidia/tegra_lic.c (revision 2ed3236082a4473c1da8f72c1ebc071a7b54321f)
1 /*-
2  * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Local interrupt controller driver for Tegra SoCs.
32  */
33 #include <sys/param.h>
34 #include <sys/module.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/rman.h>
40 
41 #include <machine/fdt.h>
42 #include <machine/intr.h>
43 #include <machine/resource.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include "pic_if.h"
49 
50 #define	LIC_VIRQ_CPU		0x00
51 #define	LIC_VIRQ_COP		0x04
52 #define	LIC_VFRQ_CPU		0x08
53 #define	LIC_VFRQ_COP		0x0c
54 #define	LIC_ISR			0x10
55 #define	LIC_FIR			0x14
56 #define	LIC_FIR_SET		0x18
57 #define	LIC_FIR_CLR		0x1c
58 #define	LIC_CPU_IER		0x20
59 #define	LIC_CPU_IER_SET		0x24
60 #define	LIC_CPU_IER_CLR		0x28
61 #define	LIC_CPU_IEP_CLASS	0x2C
62 #define	LIC_COP_IER		0x30
63 #define	LIC_COP_IER_SET		0x34
64 #define	LIC_COP_IER_CLR		0x38
65 #define	LIC_COP_IEP_CLASS	0x3c
66 
67 #define	WR4(_sc, _b, _r, _v)	bus_write_4((_sc)->mem_res[_b], (_r), (_v))
68 #define	RD4(_sc, _b, _r)	bus_read_4((_sc)->mem_res[_b], (_r))
69 
70 static struct resource_spec lic_spec[] = {
71 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
72 	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
73 	{ SYS_RES_MEMORY,	2,	RF_ACTIVE },
74 	{ SYS_RES_MEMORY,	3,	RF_ACTIVE },
75 	{ SYS_RES_MEMORY,	4,	RF_ACTIVE },
76 	{ -1, 0 }
77 };
78 
79 static struct ofw_compat_data compat_data[] = {
80 	{"nvidia,tegra124-ictlr", 	1},
81 	{NULL,				0}
82 };
83 
84 struct tegra_lic_sc {
85 	device_t		dev;
86 	struct resource		*mem_res[nitems(lic_spec)];
87 	device_t		parent;
88 };
89 
90 static int
91 tegra_lic_activate_intr(device_t dev, struct intr_irqsrc *isrc,
92     struct resource *res, struct intr_map_data *data)
93 {
94 	struct tegra_lic_sc *sc = device_get_softc(dev);
95 
96 	return (PIC_ACTIVATE_INTR(sc->parent, isrc, res, data));
97 }
98 
99 static void
100 tegra_lic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
101 {
102 	struct tegra_lic_sc *sc = device_get_softc(dev);
103 
104 	PIC_DISABLE_INTR(sc->parent, isrc);
105 }
106 
107 static void
108 tegra_lic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
109 {
110 	struct tegra_lic_sc *sc = device_get_softc(dev);
111 
112 	PIC_ENABLE_INTR(sc->parent, isrc);
113 }
114 
115 static int
116 tegra_lic_map_intr(device_t dev, struct intr_map_data *data,
117     struct intr_irqsrc **isrcp)
118 {
119 	struct tegra_lic_sc *sc = device_get_softc(dev);
120 
121 	return (PIC_MAP_INTR(sc->parent, data, isrcp));
122 }
123 
124 static int
125 tegra_lic_deactivate_intr(device_t dev, struct intr_irqsrc *isrc,
126     struct resource *res, struct intr_map_data *data)
127 {
128 	struct tegra_lic_sc *sc = device_get_softc(dev);
129 
130 	return (PIC_DEACTIVATE_INTR(sc->parent, isrc, res, data));
131 }
132 
133 static int
134 tegra_lic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
135     struct resource *res, struct intr_map_data *data)
136 {
137 	struct tegra_lic_sc *sc = device_get_softc(dev);
138 
139 	return (PIC_SETUP_INTR(sc->parent, isrc, res, data));
140 }
141 
142 static int
143 tegra_lic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
144     struct resource *res, struct intr_map_data *data)
145 {
146 	struct tegra_lic_sc *sc = device_get_softc(dev);
147 
148 	return (PIC_TEARDOWN_INTR(sc->parent, isrc, res, data));
149 }
150 
151 static void
152 tegra_lic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
153 {
154 	struct tegra_lic_sc *sc = device_get_softc(dev);
155 
156 	PIC_PRE_ITHREAD(sc->parent, isrc);
157 }
158 
159 static void
160 tegra_lic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
161 {
162 	struct tegra_lic_sc *sc = device_get_softc(dev);
163 
164 	PIC_POST_ITHREAD(sc->parent, isrc);
165 }
166 
167 static void
168 tegra_lic_post_filter(device_t dev, struct intr_irqsrc *isrc)
169 {
170 	struct tegra_lic_sc *sc = device_get_softc(dev);
171 
172 	PIC_POST_FILTER(sc->parent, isrc);
173 }
174 
175 #ifdef SMP
176 static int
177 tegra_lic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
178 {
179 	struct tegra_lic_sc *sc = device_get_softc(dev);
180 
181 	return (PIC_BIND_INTR(sc->parent, isrc));
182 }
183 #endif
184 
185 static int
186 tegra_lic_probe(device_t dev)
187 {
188 	if (!ofw_bus_status_okay(dev))
189 		return (ENXIO);
190 
191 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
192 		return (ENXIO);
193 
194 	return (BUS_PROBE_DEFAULT);
195 }
196 
197 static int
198 tegra_lic_attach(device_t dev)
199 {
200 	struct tegra_lic_sc *sc;
201 	phandle_t node;
202 	phandle_t parent_xref;
203 	int i, rv;
204 
205 	sc = device_get_softc(dev);
206 	sc->dev = dev;
207 	node = ofw_bus_get_node(dev);
208 
209 	rv = OF_getencprop(node, "interrupt-parent", &parent_xref,
210 	    sizeof(parent_xref));
211 	if (rv <= 0) {
212 		device_printf(dev, "Cannot read parent node property\n");
213 		goto fail;
214 	}
215 	sc->parent = OF_device_from_xref(parent_xref);
216 	if (sc->parent == NULL) {
217 		device_printf(dev, "Cannott find parent controller\n");
218 		goto fail;
219 	}
220 
221 	if (bus_alloc_resources(dev, lic_spec, sc->mem_res)) {
222 		device_printf(dev, "Cannott allocate resources\n");
223 		goto fail;
224 	}
225 
226 	/* Disable all interrupts, route all to irq */
227 	for (i = 0; i < nitems(lic_spec); i++) {
228 		if (sc->mem_res[i] == NULL)
229 			continue;
230 		WR4(sc, i, LIC_CPU_IER_CLR, 0xFFFFFFFF);
231 		WR4(sc, i, LIC_CPU_IEP_CLASS, 0);
232 	}
233 
234 	if (intr_pic_register(dev, OF_xref_from_node(node)) == NULL) {
235 		device_printf(dev, "Cannot register PIC\n");
236 		goto fail;
237 	}
238 	return (0);
239 
240 fail:
241 	bus_release_resources(dev, lic_spec, sc->mem_res);
242 	return (ENXIO);
243 }
244 
245 static int
246 tegra_lic_detach(device_t dev)
247 {
248 	struct tegra_lic_sc *sc;
249 	int i;
250 
251 	sc = device_get_softc(dev);
252 	for (i = 0; i < nitems(lic_spec); i++) {
253 		if (sc->mem_res[i] == NULL)
254 			continue;
255 		bus_release_resource(dev, SYS_RES_MEMORY, i,
256 		    sc->mem_res[i]);
257 	}
258 	return (0);
259 }
260 
261 static device_method_t tegra_lic_methods[] = {
262 	DEVMETHOD(device_probe,		tegra_lic_probe),
263 	DEVMETHOD(device_attach,	tegra_lic_attach),
264 	DEVMETHOD(device_detach,	tegra_lic_detach),
265 
266 	/* Interrupt controller interface */
267 	DEVMETHOD(pic_activate_intr,	tegra_lic_activate_intr),
268 	DEVMETHOD(pic_disable_intr,	tegra_lic_disable_intr),
269 	DEVMETHOD(pic_enable_intr,	tegra_lic_enable_intr),
270 	DEVMETHOD(pic_map_intr,		tegra_lic_map_intr),
271 	DEVMETHOD(pic_deactivate_intr,	tegra_lic_deactivate_intr),
272 	DEVMETHOD(pic_setup_intr,	tegra_lic_setup_intr),
273 	DEVMETHOD(pic_teardown_intr,	tegra_lic_teardown_intr),
274 	DEVMETHOD(pic_pre_ithread,	tegra_lic_pre_ithread),
275 	DEVMETHOD(pic_post_ithread,	tegra_lic_post_ithread),
276 	DEVMETHOD(pic_post_filter,	tegra_lic_post_filter),
277 #ifdef SMP
278 	DEVMETHOD(pic_bind_intr,	tegra_lic_bind_intr),
279 #endif
280 	DEVMETHOD_END
281 };
282 
283 devclass_t tegra_lic_devclass;
284 static DEFINE_CLASS_0(lic, tegra_lic_driver, tegra_lic_methods,
285     sizeof(struct tegra_lic_sc));
286 EARLY_DRIVER_MODULE(tegra_lic, simplebus, tegra_lic_driver, tegra_lic_devclass,
287     NULL, NULL, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE + 1);
288