]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_pic.c
Import to 0.6.1
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_pic.c
1 /*-
2  * Copyright 2013-2015 John Wehle <john@feith.com>
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 /*
28  * Amlogic aml8726 PIC driver.
29  *
30  * The current implementation doesn't include support for FIQ.
31  *
32  * There is a set of four interrupt controllers per cpu located in adjacent
33  * memory addresses (the set for cpu 1 starts right after the set for cpu 0)
34  * ... this allows for interrupt handling to be spread across the cpus.
35  *
36  * The multicore chips also have a GIC ... typically they run SMP kernels
37  * which include the GIC driver in which case this driver is simply used
38  * to disable the PIC.
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/bus.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/resource.h>
51 #include <sys/rman.h>
52
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55
56 #include <dev/fdt/fdt_common.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59
60 struct aml8726_pic_softc {
61         device_t                dev;
62         struct resource *       res[1];
63 };
64
65 static struct resource_spec aml8726_pic_spec[] = {
66         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
67         { -1, 0 }
68 };
69
70 /*
71  * devclass_get_device / device_get_softc could be used
72  * to dynamically locate this, however the pic is a
73  * required device which can't be unloaded so there's
74  * no need for the overhead.
75  */
76 static struct aml8726_pic_softc *aml8726_pic_sc = NULL;
77
78 #define AML_PIC_NCNTRLS         4
79 #define AML_PIC_IRQS_PER_CNTRL  32
80
81 #define AML_PIC_NIRQS           (AML_PIC_NCNTRLS * AML_PIC_IRQS_PER_CNTRL)
82
83 #define AML_PIC_0_STAT_REG      0
84 #define AML_PIC_0_STAT_CLR_REG  4
85 #define AML_PIC_0_MASK_REG      8
86 #define AML_PIC_0_FIRQ_SEL      12
87
88 #define AML_PIC_1_STAT_REG      16
89 #define AML_PIC_1_STAT_CLR_REG  20
90 #define AML_PIC_1_MASK_REG      24
91 #define AML_PIC_1_FIRQ_SEL      28
92
93 #define AML_PIC_2_STAT_REG      32
94 #define AML_PIC_2_STAT_CLR_REG  36
95 #define AML_PIC_2_MASK_REG      40
96 #define AML_PIC_2_FIRQ_SEL      44
97
98 #define AML_PIC_3_STAT_REG      48
99 #define AML_PIC_3_STAT_CLR_REG  52
100 #define AML_PIC_3_MASK_REG      56
101 #define AML_PIC_3_FIRQ_SEL      60
102
103 #define AML_PIC_CTRL(x)         ((x) >> 5)
104 #define AML_PIC_BIT(x)          (1 << ((x) & 0x1f))
105
106 #define AML_PIC_STAT_REG(x)     (AML_PIC_0_STAT_REG + AML_PIC_CTRL(x) * 16)
107 #define AML_PIC_STAT_CLR_REG(x) (AML_PIC_0_STAT_CLR_REG + AML_PIC_CTRL(x) * 16)
108 #define AML_PIC_MASK_REG(x)     (AML_PIC_0_MASK_REG + AML_PIC_CTRL(x) * 16)
109 #define AML_PIC_FIRQ_SEL(x)     (AML_PIC_0_FIRQ_REG + AML_PIC_CTRL(x) * 16)
110
111 #define CSR_WRITE_4(sc, reg, val)       bus_write_4((sc)->res[0], reg, (val))
112 #define CSR_READ_4(sc, reg)             bus_read_4((sc)->res[0], reg)
113 #define CSR_BARRIER(sc, reg)            bus_barrier((sc)->res[0], reg, 4, \
114     (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE))
115
116 static void
117 aml8726_pic_eoi(void *arg)
118 {
119         uintptr_t nb = (uintptr_t) arg;
120
121         if (nb >= AML_PIC_NIRQS)
122                 return;
123
124         arm_irq_memory_barrier(nb);
125
126         CSR_WRITE_4(aml8726_pic_sc, AML_PIC_STAT_CLR_REG(nb), AML_PIC_BIT(nb));
127
128         CSR_BARRIER(aml8726_pic_sc, AML_PIC_STAT_CLR_REG(nb));
129 }
130
131 static int
132 aml8726_pic_probe(device_t dev)
133 {
134
135         if (!ofw_bus_status_okay(dev))
136                 return (ENXIO);
137
138         if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-pic"))
139                 return (ENXIO);
140
141         device_set_desc(dev, "Amlogic aml8726 PIC");
142
143         return (BUS_PROBE_DEFAULT);
144 }
145
146 static int
147 aml8726_pic_attach(device_t dev)
148 {
149         struct aml8726_pic_softc *sc = device_get_softc(dev);
150         int i;
151
152         /* There should be exactly one instance. */
153         if (aml8726_pic_sc != NULL)
154                 return (ENXIO);
155
156         sc->dev = dev;
157
158         if (bus_alloc_resources(dev, aml8726_pic_spec, sc->res)) {
159                 device_printf(dev, "could not allocate resources for device\n");
160                 return (ENXIO);
161         }
162
163         /*
164          * Disable, clear, and set the interrupts to normal mode.
165          */
166         for (i = 0; i < AML_PIC_NCNTRLS; i++) {
167                 CSR_WRITE_4(sc, AML_PIC_0_MASK_REG + i * 16, 0);
168                 CSR_WRITE_4(sc, AML_PIC_0_STAT_CLR_REG + i * 16, ~0u);
169                 CSR_WRITE_4(sc, AML_PIC_0_FIRQ_SEL + i * 16, 0);
170         }
171
172 #ifndef DEV_GIC
173         arm_post_filter = aml8726_pic_eoi;
174 #else
175         device_printf(dev, "disabled in favor of gic\n");
176 #endif
177
178         aml8726_pic_sc = sc;
179
180         return (0);
181 }
182
183 static int
184 aml8726_pic_detach(device_t dev)
185 {
186
187         return (EBUSY);
188 }
189
190 static device_method_t aml8726_pic_methods[] = {
191         /* Device interface */
192         DEVMETHOD(device_probe,         aml8726_pic_probe),
193         DEVMETHOD(device_attach,        aml8726_pic_attach),
194         DEVMETHOD(device_detach,        aml8726_pic_detach),
195
196         DEVMETHOD_END
197 };
198
199 static driver_t aml8726_pic_driver = {
200         "pic",
201         aml8726_pic_methods,
202         sizeof(struct aml8726_pic_softc),
203 };
204
205 static devclass_t aml8726_pic_devclass;
206
207 EARLY_DRIVER_MODULE(pic, simplebus, aml8726_pic_driver, aml8726_pic_devclass,
208     0, 0, BUS_PASS_INTERRUPT);
209
210 #ifndef DEV_GIC
211 int
212 arm_get_next_irq(int last)
213 {
214         uint32_t value;
215         int irq;
216         int start;
217
218         /*
219          * The extra complexity is simply so that all IRQs are checked
220          * round robin so a particularly busy interrupt can't prevent
221          * other interrupts from being serviced.
222          */
223
224         start = (last + 1) % AML_PIC_NIRQS;
225         irq = start;
226
227         for ( ; ; ) {
228                 value = CSR_READ_4(aml8726_pic_sc, AML_PIC_STAT_REG(irq));
229
230                 for ( ; ; ) {
231                         if ((value & AML_PIC_BIT(irq)) != 0)
232                                 return (irq);
233
234                         irq = (irq + 1) % AML_PIC_NIRQS;
235
236                         if (irq == start)
237                                 return (-1);
238
239                         if ((irq % AML_PIC_IRQS_PER_CNTRL) == 0)
240                                 break;
241                 }
242         }
243 }
244
245 void
246 arm_mask_irq(uintptr_t nb)
247 {
248         uint32_t mask;
249
250         if (nb >= AML_PIC_NIRQS)
251                 return;
252
253         mask = CSR_READ_4(aml8726_pic_sc, AML_PIC_MASK_REG(nb));
254         mask &= ~AML_PIC_BIT(nb);
255         CSR_WRITE_4(aml8726_pic_sc, AML_PIC_MASK_REG(nb), mask);
256
257         CSR_BARRIER(aml8726_pic_sc, AML_PIC_MASK_REG(nb));
258
259         aml8726_pic_eoi((void *)nb);
260 }
261
262 void
263 arm_unmask_irq(uintptr_t nb)
264 {
265         uint32_t mask;
266
267         if (nb >= AML_PIC_NIRQS)
268                 return;
269
270         arm_irq_memory_barrier(nb);
271
272         mask = CSR_READ_4(aml8726_pic_sc, AML_PIC_MASK_REG(nb));
273         mask |= AML_PIC_BIT(nb);
274         CSR_WRITE_4(aml8726_pic_sc, AML_PIC_MASK_REG(nb), mask);
275
276         CSR_BARRIER(aml8726_pic_sc, AML_PIC_MASK_REG(nb));
277 }
278 #endif