]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/pl190.c
Merge ACPICA 20161222.
[FreeBSD/FreeBSD.git] / sys / arm / arm / pl190.c
1 /*-
2  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@bluezbox.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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/ktr.h>
36 #include <sys/module.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 #ifdef  DEBUG
46 #define dprintf(fmt, args...) printf(fmt, ##args)
47 #else
48 #define dprintf(fmt, args...)
49 #endif
50
51 #define VICIRQSTATUS    0x000
52 #define VICFIQSTATUS    0x004
53 #define VICRAWINTR      0x008
54 #define VICINTSELECT    0x00C
55 #define VICINTENABLE    0x010
56 #define VICINTENCLEAR   0x014
57 #define VICSOFTINT      0x018
58 #define VICSOFTINTCLEAR 0x01C
59 #define VICPROTECTION   0x020
60 #define VICPERIPHID     0xFE0
61 #define VICPRIMECELLID  0xFF0
62
63 #define VIC_NIRQS       32
64
65 struct pl190_intc_softc {
66         device_t                sc_dev;
67         struct resource *       intc_res;
68 };
69
70 static struct pl190_intc_softc *pl190_intc_sc = NULL;
71
72 #define intc_vic_read_4(reg)            \
73     bus_read_4(pl190_intc_sc->intc_res, (reg))
74 #define intc_vic_write_4(reg, val)              \
75     bus_write_4(pl190_intc_sc->intc_res, (reg), (val))
76
77 static int
78 pl190_intc_probe(device_t dev)
79 {
80
81         if (!ofw_bus_status_okay(dev))
82                 return (ENXIO);
83
84         if (!ofw_bus_is_compatible(dev, "arm,versatile-vic"))
85                 return (ENXIO);
86         device_set_desc(dev, "ARM PL190 VIC");
87         return (BUS_PROBE_DEFAULT);
88 }
89
90 static int
91 pl190_intc_attach(device_t dev)
92 {
93         struct          pl190_intc_softc *sc = device_get_softc(dev);
94         uint32_t        id;
95         int             i, rid;
96
97         sc->sc_dev = dev;
98
99         if (pl190_intc_sc)
100                 return (ENXIO);
101
102         /* Request memory resources */
103         rid = 0;
104         sc->intc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
105             RF_ACTIVE);
106         if (sc->intc_res == NULL) {
107                 device_printf(dev, "Error: could not allocate memory resources\n");
108                 return (ENXIO);
109         }
110
111         pl190_intc_sc = sc;
112         /*
113          * All interrupts should use IRQ line
114          */
115         intc_vic_write_4(VICINTSELECT, 0x00000000);
116         /* Disable all interrupts */
117         intc_vic_write_4(VICINTENCLEAR, 0xffffffff);
118         /* Enable INT31, SIC IRQ */
119         intc_vic_write_4(VICINTENABLE, (1U << 31));
120
121         id = 0;
122         for (i = 3; i >= 0; i--) {
123                 id = (id << 8) |
124                      (intc_vic_read_4(VICPERIPHID + i*4) & 0xff);
125         }
126
127         device_printf(dev, "Peripheral ID: %08x\n", id);
128
129         id = 0;
130         for (i = 3; i >= 0; i--) {
131                 id = (id << 8) |
132                      (intc_vic_read_4(VICPRIMECELLID + i*4) & 0xff);
133         }
134
135         device_printf(dev, "PrimeCell ID: %08x\n", id);
136
137         return (0);
138 }
139
140 static device_method_t pl190_intc_methods[] = {
141         DEVMETHOD(device_probe,         pl190_intc_probe),
142         DEVMETHOD(device_attach,        pl190_intc_attach),
143         { 0, 0 }
144 };
145
146 static driver_t pl190_intc_driver = {
147         "intc",
148         pl190_intc_methods,
149         sizeof(struct pl190_intc_softc),
150 };
151
152 static devclass_t pl190_intc_devclass;
153
154 EARLY_DRIVER_MODULE(intc, simplebus, pl190_intc_driver, pl190_intc_devclass,
155     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
156
157 int
158 arm_get_next_irq(int last_irq)
159 {
160         uint32_t pending;
161         int32_t irq = last_irq + 1;
162
163         /* Sanity check */
164         if (irq < 0)
165                 irq = 0;
166
167         pending = intc_vic_read_4(VICIRQSTATUS);
168         while (irq < VIC_NIRQS) {
169                 if (pending & (1 << irq))
170                         return (irq);
171                 irq++;
172         }
173
174         return (-1);
175 }
176
177 void
178 arm_mask_irq(uintptr_t nb)
179 {
180
181         dprintf("%s: %d\n", __func__, nb);
182         intc_vic_write_4(VICINTENCLEAR, (1 << nb));
183 }
184
185 void
186 arm_unmask_irq(uintptr_t nb)
187 {
188
189         dprintf("%s: %d\n", __func__, nb);
190         intc_vic_write_4(VICINTENABLE, (1 << nb));
191 }