1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright 2003 by Peter Grehan. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/openfirm.h>
41
42 #include <machine/bus.h>
43 #include <machine/intr_machdep.h>
44 #include <machine/md_var.h>
45 #include <machine/pio.h>
46 #include <machine/resource.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50
51 #include <sys/rman.h>
52
53 #include <machine/openpicreg.h>
54 #include <machine/openpicvar.h>
55
56 #include "pic_if.h"
57
58 /*
59 * OFW interface
60 */
61 static int openpic_ofw_probe(device_t);
62 static int openpic_ofw_attach(device_t);
63
64 static void openpic_ofw_translate_code(device_t, u_int irq, int code,
65 enum intr_trigger *trig, enum intr_polarity *pol);
66
67 static device_method_t openpic_ofw_methods[] = {
68 /* Device interface */
69 DEVMETHOD(device_probe, openpic_ofw_probe),
70 DEVMETHOD(device_attach, openpic_ofw_attach),
71
72 /* PIC interface */
73 DEVMETHOD(pic_translate_code, openpic_ofw_translate_code),
74
75 DEVMETHOD_END
76 };
77
78 DEFINE_CLASS_1(openpic, openpic_ofw_driver, openpic_ofw_methods,
79 sizeof(struct openpic_softc), openpic_class);
80
81 EARLY_DRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, 0, 0,
82 BUS_PASS_INTERRUPT);
83 EARLY_DRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, 0, 0,
84 BUS_PASS_INTERRUPT);
85 EARLY_DRIVER_MODULE(openpic, macio, openpic_ofw_driver, 0, 0,
86 BUS_PASS_INTERRUPT);
87
88 static int
openpic_ofw_probe(device_t dev)89 openpic_ofw_probe(device_t dev)
90 {
91 const char *type = ofw_bus_get_type(dev);
92
93 if (type == NULL)
94 return (ENXIO);
95
96 if (!ofw_bus_is_compatible(dev, "chrp,open-pic") &&
97 strcmp(type, "open-pic") != 0)
98 return (ENXIO);
99
100 /*
101 * On some U4 systems, there is a phantom MPIC in the mac-io cell.
102 * The uninorth driver will pick up the real PIC, so ignore it here.
103 */
104 if (OF_finddevice("/u4") != (phandle_t)-1)
105 return (ENXIO);
106
107 device_set_desc(dev, OPENPIC_DEVSTR);
108 return (0);
109 }
110
111 static int
openpic_ofw_attach(device_t dev)112 openpic_ofw_attach(device_t dev)
113 {
114 struct openpic_softc *sc;
115 phandle_t xref, node;
116
117 node = ofw_bus_get_node(dev);
118 sc = device_get_softc(dev);
119
120 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
121 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
122 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
123 xref = node;
124
125 if (ofw_bus_is_compatible(dev, "fsl,mpic")) {
126 sc->sc_quirks = OPENPIC_QUIRK_SINGLE_BIND;
127 sc->sc_quirks |= OPENPIC_QUIRK_HIDDEN_IRQS;
128 }
129
130 return (openpic_common_attach(dev, xref));
131 }
132
133 static void
openpic_ofw_translate_code(device_t dev,u_int irq,int code,enum intr_trigger * trig,enum intr_polarity * pol)134 openpic_ofw_translate_code(device_t dev, u_int irq, int code,
135 enum intr_trigger *trig, enum intr_polarity *pol)
136 {
137 switch (code) {
138 case 0:
139 /* L to H edge */
140 *trig = INTR_TRIGGER_EDGE;
141 *pol = INTR_POLARITY_HIGH;
142 break;
143 case 1:
144 /* Active L level */
145 *trig = INTR_TRIGGER_LEVEL;
146 *pol = INTR_POLARITY_LOW;
147 break;
148 case 2:
149 /* Active H level */
150 *trig = INTR_TRIGGER_LEVEL;
151 *pol = INTR_POLARITY_HIGH;
152 break;
153 case 3:
154 /* H to L edge */
155 *trig = INTR_TRIGGER_EDGE;
156 *pol = INTR_POLARITY_LOW;
157 break;
158 default:
159 *trig = INTR_TRIGGER_CONFORM;
160 *pol = INTR_POLARITY_CONFORM;
161 }
162 }
163