]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/lpc/lpc_intc.c
Add efi.8 as a man page link to uefi.8
[FreeBSD/FreeBSD.git] / sys / arm / lpc / lpc_intc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Jakub Wojciech Klama <jceel@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/rman.h>
40 #include <sys/timetc.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
46
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49
50 #include <arm/lpc/lpcreg.h>
51
52 struct lpc_intc_softc {
53         struct resource *       li_res;
54         bus_space_tag_t         li_bst;
55         bus_space_handle_t      li_bsh;
56 };
57
58 static int lpc_intc_probe(device_t);
59 static int lpc_intc_attach(device_t);
60 static void lpc_intc_eoi(void *);
61
62 static struct lpc_intc_softc *intc_softc = NULL;
63
64 #define intc_read_4(_sc, _reg)          \
65     bus_space_read_4((_sc)->li_bst, (_sc)->li_bsh, (_reg))
66 #define intc_write_4(_sc, _reg, _val)           \
67     bus_space_write_4((_sc)->li_bst, (_sc)->li_bsh, (_reg), (_val))
68
69 static int
70 lpc_intc_probe(device_t dev)
71 {
72
73         if (!ofw_bus_status_okay(dev))
74                 return (ENXIO);
75
76         if (!ofw_bus_is_compatible(dev, "lpc,pic"))
77                 return (ENXIO);
78
79         device_set_desc(dev, "LPC32x0 Interrupt Controller");
80         return (BUS_PROBE_DEFAULT);
81 }
82
83 static int
84 lpc_intc_attach(device_t dev)
85 {
86         struct lpc_intc_softc *sc = device_get_softc(dev);
87         int rid = 0;
88
89         if (intc_softc)
90                 return (ENXIO);
91
92         sc->li_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 
93             RF_ACTIVE);
94         if (!sc->li_res) {
95                 device_printf(dev, "could not alloc resources\n");
96                 return (ENXIO);
97         }
98
99         sc->li_bst = rman_get_bustag(sc->li_res);
100         sc->li_bsh = rman_get_bushandle(sc->li_res);
101         intc_softc = sc;
102         arm_post_filter = lpc_intc_eoi;
103
104         /* Clear interrupt status registers and disable all interrupts */
105         intc_write_4(sc, LPC_INTC_MIC_ER, 0);
106         intc_write_4(sc, LPC_INTC_SIC1_ER, 0);
107         intc_write_4(sc, LPC_INTC_SIC2_ER, 0);
108         intc_write_4(sc, LPC_INTC_MIC_RSR, ~0);
109         intc_write_4(sc, LPC_INTC_SIC1_RSR, ~0);
110         intc_write_4(sc, LPC_INTC_SIC2_RSR, ~0);
111         return (0);
112 }
113
114 static device_method_t lpc_intc_methods[] = {
115         DEVMETHOD(device_probe,         lpc_intc_probe),
116         DEVMETHOD(device_attach,        lpc_intc_attach),
117         { 0, 0 }
118 };
119
120 static driver_t lpc_intc_driver = {
121         "pic",
122         lpc_intc_methods,
123         sizeof(struct lpc_intc_softc),
124 };
125
126 static devclass_t lpc_intc_devclass;
127
128 DRIVER_MODULE(pic, simplebus, lpc_intc_driver, lpc_intc_devclass, 0, 0);
129
130 int
131 arm_get_next_irq(int last)
132 {
133         struct lpc_intc_softc *sc = intc_softc;
134         uint32_t value;
135         int i;
136
137         /* IRQs 0-31 are mapped to LPC_INTC_MIC_SR */
138         value = intc_read_4(sc, LPC_INTC_MIC_SR);
139         for (i = 0; i < 32; i++) {
140                 if (value & (1 << i))
141                         return (i);
142         }
143
144         /* IRQs 32-63 are mapped to LPC_INTC_SIC1_SR */
145         value = intc_read_4(sc, LPC_INTC_SIC1_SR);
146         for (i = 0; i < 32; i++) {
147                 if (value & (1 << i))
148                         return (i + 32);
149         }
150
151         /* IRQs 64-95 are mapped to LPC_INTC_SIC2_SR */
152         value = intc_read_4(sc, LPC_INTC_SIC2_SR);
153         for (i = 0; i < 32; i++) {
154                 if (value & (1 << i))
155                         return (i + 64);
156         }
157
158         return (-1);
159 }
160
161 void
162 arm_mask_irq(uintptr_t nb)
163 {
164         struct lpc_intc_softc *sc = intc_softc;
165         int reg;
166         uint32_t value;
167
168         /* Make sure that interrupt isn't active already */
169         lpc_intc_eoi((void *)nb);
170
171         if (nb > 63) {
172                 nb -= 64;
173                 reg = LPC_INTC_SIC2_ER;
174         } else if (nb > 31) {
175                 nb -= 32;
176                 reg = LPC_INTC_SIC1_ER;
177         } else
178                 reg = LPC_INTC_MIC_ER;
179
180         /* Clear bit in ER register */
181         value = intc_read_4(sc, reg);
182         value &= ~(1 << nb);
183         intc_write_4(sc, reg, value);
184 }
185
186 void
187 arm_unmask_irq(uintptr_t nb)
188 {
189         struct lpc_intc_softc *sc = intc_softc;
190         int reg;
191         uint32_t value;
192
193         if (nb > 63) {
194                 nb -= 64;
195                 reg = LPC_INTC_SIC2_ER;
196         } else if (nb > 31) {
197                 nb -= 32;
198                 reg = LPC_INTC_SIC1_ER;
199         } else
200                 reg = LPC_INTC_MIC_ER;
201
202         /* Set bit in ER register */
203         value = intc_read_4(sc, reg);
204         value |= (1 << nb);
205         intc_write_4(sc, reg, value);
206 }
207
208 static void
209 lpc_intc_eoi(void *data)
210 {
211         struct lpc_intc_softc *sc = intc_softc;
212         int reg;
213         int nb = (int)data;
214         uint32_t value;
215
216         if (nb > 63) {
217                 nb -= 64;
218                 reg = LPC_INTC_SIC2_RSR;
219         } else if (nb > 31) {
220                 nb -= 32;
221                 reg = LPC_INTC_SIC1_RSR;
222         } else
223                 reg = LPC_INTC_MIC_RSR;
224
225         /* Set bit in RSR register */
226         value = intc_read_4(sc, reg);
227         value |= (1 << nb);
228         intc_write_4(sc, reg, value);
229
230 }
231
232 #ifndef INTRNG
233 static int
234 fdt_pic_decode_ic(phandle_t node, pcell_t *intr, int *interrupt, int *trig,
235     int *pol)
236 {
237         if (!ofw_bus_node_is_compatible(node, "lpc,pic"))
238                 return (ENXIO);
239
240         *interrupt = fdt32_to_cpu(intr[0]);
241         *trig = INTR_TRIGGER_CONFORM;
242         *pol = INTR_POLARITY_CONFORM;
243         return (0);
244 }
245
246 fdt_pic_decode_t fdt_pic_table[] = {
247         &fdt_pic_decode_ic,
248         NULL
249 };
250 #endif