xref: /freebsd/sys/powerpc/ofw/openpic_ofw.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 	DEVMETHOD(device_suspend,	openpic_suspend),
72 	DEVMETHOD(device_resume,	openpic_resume),
73 
74 	/* PIC interface */
75 	DEVMETHOD(pic_bind,		openpic_bind),
76 	DEVMETHOD(pic_config,		openpic_config),
77 	DEVMETHOD(pic_dispatch,		openpic_dispatch),
78 	DEVMETHOD(pic_enable,		openpic_enable),
79 	DEVMETHOD(pic_eoi,		openpic_eoi),
80 	DEVMETHOD(pic_ipi,		openpic_ipi),
81 	DEVMETHOD(pic_mask,		openpic_mask),
82 	DEVMETHOD(pic_unmask,		openpic_unmask),
83 
84 	DEVMETHOD(pic_translate_code,	openpic_ofw_translate_code),
85 
86 	DEVMETHOD_END
87 };
88 
89 static driver_t openpic_ofw_driver = {
90 	"openpic",
91 	openpic_ofw_methods,
92 	sizeof(struct openpic_softc),
93 };
94 
95 EARLY_DRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, 0, 0,
96     BUS_PASS_INTERRUPT);
97 EARLY_DRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, 0, 0,
98     BUS_PASS_INTERRUPT);
99 EARLY_DRIVER_MODULE(openpic, macio, openpic_ofw_driver, 0, 0,
100     BUS_PASS_INTERRUPT);
101 
102 static int
103 openpic_ofw_probe(device_t dev)
104 {
105 	const char *type = ofw_bus_get_type(dev);
106 
107 	if (type == NULL)
108                 return (ENXIO);
109 
110 	if (!ofw_bus_is_compatible(dev, "chrp,open-pic") &&
111 	    strcmp(type, "open-pic") != 0)
112                 return (ENXIO);
113 
114 	/*
115 	 * On some U4 systems, there is a phantom MPIC in the mac-io cell.
116 	 * The uninorth driver will pick up the real PIC, so ignore it here.
117 	 */
118 	if (OF_finddevice("/u4") != (phandle_t)-1)
119 		return (ENXIO);
120 
121 	device_set_desc(dev, OPENPIC_DEVSTR);
122 	return (0);
123 }
124 
125 static int
126 openpic_ofw_attach(device_t dev)
127 {
128 	struct openpic_softc *sc;
129 	phandle_t xref, node;
130 
131 	node = ofw_bus_get_node(dev);
132 	sc = device_get_softc(dev);
133 
134 	if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
135 	    OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
136 	    OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
137 		xref = node;
138 
139 	if (ofw_bus_is_compatible(dev, "fsl,mpic")) {
140 		sc->sc_quirks = OPENPIC_QUIRK_SINGLE_BIND;
141 		sc->sc_quirks |= OPENPIC_QUIRK_HIDDEN_IRQS;
142 	}
143 
144 	return (openpic_common_attach(dev, xref));
145 }
146 
147 static void
148 openpic_ofw_translate_code(device_t dev, u_int irq, int code,
149     enum intr_trigger *trig, enum intr_polarity *pol)
150 {
151 	switch (code) {
152 	case 0:
153 		/* L to H edge */
154 		*trig = INTR_TRIGGER_EDGE;
155 		*pol = INTR_POLARITY_HIGH;
156 		break;
157 	case 1:
158 		/* Active L level */
159 		*trig = INTR_TRIGGER_LEVEL;
160 		*pol = INTR_POLARITY_LOW;
161 		break;
162 	case 2:
163 		/* Active H level */
164 		*trig = INTR_TRIGGER_LEVEL;
165 		*pol = INTR_POLARITY_HIGH;
166 		break;
167 	case 3:
168 		/* H to L edge */
169 		*trig = INTR_TRIGGER_EDGE;
170 		*pol = INTR_POLARITY_LOW;
171 		break;
172 	default:
173 		*trig = INTR_TRIGGER_CONFORM;
174 		*pol = INTR_POLARITY_CONFORM;
175 	}
176 }
177