]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/versatile/versatile_sic.c
Upgrade to OpenSSH 7.5p1.
[FreeBSD/FreeBSD.git] / sys / arm / versatile / versatile_sic.c
1 /*-
2  * Copyright (c) 2012-2017 Oleksandr Tymoshenko <gonzo@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 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/ktr.h>
35 #include <sys/module.h>
36 #include <sys/proc.h>
37 #include <sys/rman.h>
38 #include <machine/bus.h>
39 #include <machine/intr.h>
40
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44
45 #include "pic_if.h"
46
47 #ifdef  DEBUG
48 #define dprintf(fmt, args...) printf(fmt, ##args)
49 #else
50 #define dprintf(fmt, args...)
51 #endif
52
53 #define SIC_STATUS      0x00
54 #define SIC_RAWSTAT     0x04
55 #define SIC_ENABLE      0x08
56 #define SIC_ENSET       0x08
57 #define SIC_ENCLR       0x0C
58 #define SIC_SOFTINTSET  0x10
59 #define SIC_SOFTINTCLR  0x14
60 #define SIC_PICENABLE   0x20
61 #define SIC_PICENSET    0x20
62 #define SIC_PICENCLR    0x24
63
64 #define SIC_NIRQS       32
65
66 struct versatile_sic_irqsrc {
67         struct intr_irqsrc              isrc;
68         u_int                           irq;
69 };
70
71 struct versatile_sic_softc {
72         device_t                dev;
73         struct mtx              mtx;
74         struct resource *       mem_res;
75         struct resource *       irq_res;
76         void                    *intrh;
77         struct versatile_sic_irqsrc     isrcs[SIC_NIRQS];
78 };
79
80 #define SIC_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx)
81 #define SIC_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx)
82
83 #define SIC_READ_4(sc, reg)                     \
84     bus_read_4(sc->mem_res, (reg))
85 #define SIC_WRITE_4(sc, reg, val)               \
86     bus_write_4(sc->mem_res, (reg), (val))
87
88 /*
89  * Driver stuff
90  */
91 static int versatile_sic_probe(device_t);
92 static int versatile_sic_attach(device_t);
93 static int versatile_sic_detach(device_t);
94
95 static void
96 versatile_sic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
97 {
98         struct versatile_sic_softc *sc;
99         struct versatile_sic_irqsrc *src;
100
101         sc = device_get_softc(dev);
102         src = (struct versatile_sic_irqsrc *)isrc;
103
104         SIC_LOCK(sc);
105         SIC_WRITE_4(sc, SIC_ENCLR, (1 << src->irq));
106         SIC_UNLOCK(sc);
107 }
108
109 static void
110 versatile_sic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
111 {
112         struct versatile_sic_softc *sc;
113         struct versatile_sic_irqsrc *src;
114
115         sc = device_get_softc(dev);
116         src = (struct versatile_sic_irqsrc *)isrc;
117
118         SIC_LOCK(sc);
119         SIC_WRITE_4(sc, SIC_ENSET, (1 << src->irq));
120         SIC_UNLOCK(sc);
121 }
122
123 static int
124 versatile_sic_map_intr(device_t dev, struct intr_map_data *data,
125     struct intr_irqsrc **isrcp)
126 {
127         struct intr_map_data_fdt *daf;
128         struct versatile_sic_softc *sc;
129
130         if (data->type != INTR_MAP_DATA_FDT)
131                 return (ENOTSUP);
132
133         daf = (struct intr_map_data_fdt *)data;
134         if (daf->ncells != 1 || daf->cells[0] >= SIC_NIRQS)
135                 return (EINVAL);
136
137         sc = device_get_softc(dev);
138         *isrcp = &sc->isrcs[daf->cells[0]].isrc;
139         return (0);
140 }
141
142 static void
143 versatile_sic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
144 {
145         versatile_sic_disable_intr(dev, isrc);
146 }
147
148 static void
149 versatile_sic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
150 {
151         struct versatile_sic_irqsrc *src;
152
153         src = (struct versatile_sic_irqsrc *)isrc;
154         arm_irq_memory_barrier(src->irq);
155         versatile_sic_enable_intr(dev, isrc);
156 }
157
158 static void
159 versatile_sic_post_filter(device_t dev, struct intr_irqsrc *isrc)
160 {
161         struct versatile_sic_irqsrc *src;
162
163         src = (struct versatile_sic_irqsrc *)isrc;
164         arm_irq_memory_barrier(src->irq);
165 }
166
167 static int
168 versatile_sic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
169     struct resource *res, struct intr_map_data *data)
170 {
171
172         return (0);
173 }
174
175 static int
176 versatile_sic_filter(void *arg)
177 {
178         struct versatile_sic_softc *sc;
179         struct intr_irqsrc *isrc;
180         uint32_t i, interrupts;
181
182         sc = arg;
183         SIC_LOCK(sc);
184         interrupts = SIC_READ_4(sc, SIC_STATUS);
185         SIC_UNLOCK(sc);
186         for (i = 0; interrupts != 0; i++, interrupts >>= 1) {
187                 if ((interrupts & 0x1) == 0)
188                         continue;
189                 isrc = &sc->isrcs[i].isrc;
190                 if (intr_isrc_dispatch(isrc, curthread->td_intr_frame) != 0) {
191                         versatile_sic_disable_intr(sc->dev, isrc);
192                         versatile_sic_post_filter(sc->dev, isrc);
193                         device_printf(sc->dev, "Stray irq %u disabled\n", i);
194                 }
195         }
196
197         return (FILTER_HANDLED);
198 }
199
200 static int
201 versatile_sic_probe(device_t dev)
202 {
203
204         if (!ofw_bus_status_okay(dev))
205                 return (ENXIO);
206
207         if (!ofw_bus_is_compatible(dev, "arm,versatile-sic"))
208                 return (ENXIO);
209         device_set_desc(dev, "ARM Versatile SIC");
210         return (BUS_PROBE_DEFAULT);
211 }
212
213 static int
214 versatile_sic_attach(device_t dev)
215 {
216         struct          versatile_sic_softc *sc = device_get_softc(dev);
217         int             rid, error;
218         uint32_t        irq;
219         const char      *name;
220         struct          versatile_sic_irqsrc *isrcs;
221
222         sc->dev = dev;
223         mtx_init(&sc->mtx, device_get_nameunit(dev), "sic",
224             MTX_SPIN);
225
226         /* Request memory resources */
227         rid = 0;
228         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
229             RF_ACTIVE);
230         if (sc->mem_res == NULL) {
231                 device_printf(dev, "Error: could not allocate memory resources\n");
232                 return (ENXIO);
233         }
234
235         /* Request memory resources */
236         rid = 0;
237         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
238             RF_ACTIVE);
239         if (sc->irq_res == NULL) {
240                 device_printf(dev, "could not allocate IRQ resources\n");
241                 versatile_sic_detach(dev);
242                 return (ENXIO);
243         }
244
245         if ((bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
246             versatile_sic_filter, NULL, sc, &sc->intrh))) {
247                 device_printf(dev,
248                     "unable to register interrupt handler\n");
249                 versatile_sic_detach(dev);
250                 return (ENXIO);
251         }
252
253         /* Disable all interrupts on SIC */
254         SIC_WRITE_4(sc, SIC_ENCLR, 0xffffffff);
255
256         /* PIC attachment */
257         isrcs = sc->isrcs;
258         name = device_get_nameunit(sc->dev);
259         for (irq = 0; irq < SIC_NIRQS; irq++) {
260                 isrcs[irq].irq = irq;
261                 error = intr_isrc_register(&isrcs[irq].isrc, sc->dev,
262                     0, "%s,%u", name, irq);
263                 if (error != 0)
264                         return (error);
265         }
266
267         intr_pic_register(dev, OF_xref_from_node(ofw_bus_get_node(dev)));
268
269         return (0);
270 }
271
272 static int
273 versatile_sic_detach(device_t dev)
274 {
275         struct          versatile_sic_softc *sc;
276
277         sc = device_get_softc(dev);
278
279         if (sc->intrh)
280                 bus_teardown_intr(dev, sc->irq_res, sc->intrh);
281
282         if (sc->mem_res == NULL)
283                 bus_release_resource(dev, SYS_RES_MEMORY,
284                         rman_get_rid(sc->mem_res), sc->mem_res);
285
286         if (sc->irq_res == NULL)
287                 bus_release_resource(dev, SYS_RES_IRQ,
288                         rman_get_rid(sc->irq_res), sc->irq_res);
289
290         mtx_destroy(&sc->mtx);
291
292         return (0);
293
294 }
295
296 static device_method_t versatile_sic_methods[] = {
297         DEVMETHOD(device_probe,         versatile_sic_probe),
298         DEVMETHOD(device_attach,        versatile_sic_attach),
299         DEVMETHOD(device_detach,        versatile_sic_detach),
300
301         DEVMETHOD(pic_disable_intr,     versatile_sic_disable_intr),
302         DEVMETHOD(pic_enable_intr,      versatile_sic_enable_intr),
303         DEVMETHOD(pic_map_intr,         versatile_sic_map_intr),
304         DEVMETHOD(pic_post_filter,      versatile_sic_post_filter),
305         DEVMETHOD(pic_post_ithread,     versatile_sic_post_ithread),
306         DEVMETHOD(pic_pre_ithread,      versatile_sic_pre_ithread),
307         DEVMETHOD(pic_setup_intr,       versatile_sic_setup_intr),
308
309         DEVMETHOD_END
310 };
311
312 static driver_t versatile_sic_driver = {
313         "sic",
314         versatile_sic_methods,
315         sizeof(struct versatile_sic_softc),
316 };
317
318 static devclass_t versatile_sic_devclass;
319
320 DRIVER_MODULE(sic, simplebus, versatile_sic_driver, versatile_sic_devclass, 0, 0);