xref: /freebsd/sys/powerpc/ofw/openpic_ofw.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/openfirm.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
48 #include <machine/pio.h>
49 #include <machine/resource.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 
54 #include <sys/rman.h>
55 
56 #include <machine/openpicreg.h>
57 #include <machine/openpicvar.h>
58 
59 #include "pic_if.h"
60 
61 /*
62  * OFW interface
63  */
64 static int	openpic_ofw_probe(device_t);
65 static int	openpic_ofw_attach(device_t);
66 
67 static void	openpic_ofw_translate_code(device_t, u_int irq, int code,
68 		    enum intr_trigger *trig, enum intr_polarity *pol);
69 
70 static device_method_t  openpic_ofw_methods[] = {
71 	/* Device interface */
72 	DEVMETHOD(device_probe,		openpic_ofw_probe),
73 	DEVMETHOD(device_attach,	openpic_ofw_attach),
74 	DEVMETHOD(device_suspend,	openpic_suspend),
75 	DEVMETHOD(device_resume,	openpic_resume),
76 
77 	/* PIC interface */
78 	DEVMETHOD(pic_bind,		openpic_bind),
79 	DEVMETHOD(pic_config,		openpic_config),
80 	DEVMETHOD(pic_dispatch,		openpic_dispatch),
81 	DEVMETHOD(pic_enable,		openpic_enable),
82 	DEVMETHOD(pic_eoi,		openpic_eoi),
83 	DEVMETHOD(pic_ipi,		openpic_ipi),
84 	DEVMETHOD(pic_mask,		openpic_mask),
85 	DEVMETHOD(pic_unmask,		openpic_unmask),
86 
87 	DEVMETHOD(pic_translate_code,	openpic_ofw_translate_code),
88 
89 	DEVMETHOD_END
90 };
91 
92 static driver_t openpic_ofw_driver = {
93 	"openpic",
94 	openpic_ofw_methods,
95 	sizeof(struct openpic_softc),
96 };
97 
98 EARLY_DRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, openpic_devclass,
99     0, 0, BUS_PASS_INTERRUPT);
100 EARLY_DRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, openpic_devclass,
101     0, 0, BUS_PASS_INTERRUPT);
102 EARLY_DRIVER_MODULE(openpic, macio, openpic_ofw_driver, openpic_devclass, 0, 0,
103     BUS_PASS_INTERRUPT);
104 
105 static int
106 openpic_ofw_probe(device_t dev)
107 {
108 	const char *type = ofw_bus_get_type(dev);
109 
110 	if (type == NULL)
111                 return (ENXIO);
112 
113 	if (!ofw_bus_is_compatible(dev, "chrp,open-pic") &&
114 	    strcmp(type, "open-pic") != 0)
115                 return (ENXIO);
116 
117 	/*
118 	 * On some U4 systems, there is a phantom MPIC in the mac-io cell.
119 	 * The uninorth driver will pick up the real PIC, so ignore it here.
120 	 */
121 	if (OF_finddevice("/u4") != (phandle_t)-1)
122 		return (ENXIO);
123 
124 	device_set_desc(dev, OPENPIC_DEVSTR);
125 	return (0);
126 }
127 
128 static int
129 openpic_ofw_attach(device_t dev)
130 {
131 	struct openpic_softc *sc;
132 	phandle_t xref, node;
133 
134 	node = ofw_bus_get_node(dev);
135 	sc = device_get_softc(dev);
136 
137 	if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
138 	    OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
139 	    OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
140 		xref = node;
141 
142 	if (ofw_bus_is_compatible(dev, "fsl,mpic")) {
143 		sc->sc_quirks = OPENPIC_QUIRK_SINGLE_BIND;
144 		sc->sc_quirks |= OPENPIC_QUIRK_HIDDEN_IRQS;
145 	}
146 
147 	return (openpic_common_attach(dev, xref));
148 }
149 
150 static void
151 openpic_ofw_translate_code(device_t dev, u_int irq, int code,
152     enum intr_trigger *trig, enum intr_polarity *pol)
153 {
154 	switch (code) {
155 	case 0:
156 		/* L to H edge */
157 		*trig = INTR_TRIGGER_EDGE;
158 		*pol = INTR_POLARITY_HIGH;
159 		break;
160 	case 1:
161 		/* Active L level */
162 		*trig = INTR_TRIGGER_LEVEL;
163 		*pol = INTR_POLARITY_LOW;
164 		break;
165 	case 2:
166 		/* Active H level */
167 		*trig = INTR_TRIGGER_LEVEL;
168 		*pol = INTR_POLARITY_HIGH;
169 		break;
170 	case 3:
171 		/* H to L edge */
172 		*trig = INTR_TRIGGER_EDGE;
173 		*pol = INTR_POLARITY_LOW;
174 		break;
175 	default:
176 		*trig = INTR_TRIGGER_CONFORM;
177 		*pol = INTR_POLARITY_CONFORM;
178 	}
179 }
180