1139825Simp/*-
2124469Sgrehan * Copyright 2003 by Peter Grehan. All rights reserved.
3124469Sgrehan *
4124469Sgrehan * Redistribution and use in source and binary forms, with or without
5124469Sgrehan * modification, are permitted provided that the following conditions
6124469Sgrehan * are met:
7124469Sgrehan * 1. Redistributions of source code must retain the above copyright
8124469Sgrehan *    notice, this list of conditions and the following disclaimer.
9124469Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
10124469Sgrehan *    notice, this list of conditions and the following disclaimer in the
11124469Sgrehan *    documentation and/or other materials provided with the distribution.
12124469Sgrehan * 3. The name of the author may not be used to endorse or promote products
13124469Sgrehan *    derived from this software without specific prior written permission.
14124469Sgrehan *
15124469Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16124469Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17124469Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18124469Sgrehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19124469Sgrehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20124469Sgrehan * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21124469Sgrehan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22124469Sgrehan * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23124469Sgrehan * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24124469Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25124469Sgrehan * SUCH DAMAGE.
26124469Sgrehan *
27124469Sgrehan */
28124469Sgrehan
29124469Sgrehan#include <sys/cdefs.h>
30124469Sgrehan__FBSDID("$FreeBSD$");
31124469Sgrehan
32124469Sgrehan#include <sys/param.h>
33124469Sgrehan#include <sys/systm.h>
34131102Sgrehan#include <sys/module.h>
35124469Sgrehan#include <sys/bus.h>
36124469Sgrehan#include <sys/conf.h>
37124469Sgrehan#include <sys/kernel.h>
38124469Sgrehan
39133589Smarius#include <dev/ofw/ofw_bus.h>
40256898Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
41124469Sgrehan#include <dev/ofw/openfirm.h>
42124469Sgrehan
43124469Sgrehan#include <machine/bus.h>
44124469Sgrehan#include <machine/intr_machdep.h>
45124469Sgrehan#include <machine/md_var.h>
46124469Sgrehan#include <machine/pio.h>
47124469Sgrehan#include <machine/resource.h>
48124469Sgrehan
49124469Sgrehan#include <vm/vm.h>
50124469Sgrehan#include <vm/pmap.h>
51124469Sgrehan
52124469Sgrehan#include <sys/rman.h>
53124469Sgrehan
54124469Sgrehan#include <machine/openpicvar.h>
55124469Sgrehan
56124469Sgrehan#include "pic_if.h"
57124469Sgrehan
58124469Sgrehan/*
59256898Snwhitehorn * OFW interface
60124469Sgrehan */
61256898Snwhitehornstatic int	openpic_ofw_probe(device_t);
62256898Snwhitehornstatic int	openpic_ofw_attach(device_t);
63124469Sgrehan
64265969Sianstatic void	openpic_ofw_translate_code(device_t, u_int irq, int code,
65265969Sian		    enum intr_trigger *trig, enum intr_polarity *pol);
66265969Sian
67256898Snwhitehornstatic device_method_t  openpic_ofw_methods[] = {
68124469Sgrehan	/* Device interface */
69256898Snwhitehorn	DEVMETHOD(device_probe,		openpic_ofw_probe),
70256898Snwhitehorn	DEVMETHOD(device_attach,	openpic_ofw_attach),
71124469Sgrehan
72124469Sgrehan	/* PIC interface */
73209486Snwhitehorn	DEVMETHOD(pic_bind,		openpic_bind),
74176918Smarcel	DEVMETHOD(pic_config,		openpic_config),
75171805Smarcel	DEVMETHOD(pic_dispatch,		openpic_dispatch),
76171805Smarcel	DEVMETHOD(pic_enable,		openpic_enable),
77171805Smarcel	DEVMETHOD(pic_eoi,		openpic_eoi),
78176208Smarcel	DEVMETHOD(pic_ipi,		openpic_ipi),
79171805Smarcel	DEVMETHOD(pic_mask,		openpic_mask),
80171805Smarcel	DEVMETHOD(pic_unmask,		openpic_unmask),
81124469Sgrehan
82265969Sian	DEVMETHOD(pic_translate_code,	openpic_ofw_translate_code),
83265969Sian
84256898Snwhitehorn	DEVMETHOD_END
85124469Sgrehan};
86124469Sgrehan
87256898Snwhitehornstatic driver_t openpic_ofw_driver = {
88171805Smarcel	"openpic",
89256898Snwhitehorn	openpic_ofw_methods,
90171805Smarcel	sizeof(struct openpic_softc),
91124469Sgrehan};
92124469Sgrehan
93266160SianDRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, openpic_devclass, 0, 0);
94256898SnwhitehornDRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, openpic_devclass, 0, 0);
95256898SnwhitehornDRIVER_MODULE(openpic, macio, openpic_ofw_driver, openpic_devclass, 0, 0);
96124469Sgrehan
97124469Sgrehanstatic int
98256898Snwhitehornopenpic_ofw_probe(device_t dev)
99124469Sgrehan{
100133589Smarius	const char *type = ofw_bus_get_type(dev);
101124469Sgrehan
102256898Snwhitehorn	if (type == NULL)
103124469Sgrehan                return (ENXIO);
104124469Sgrehan
105256898Snwhitehorn	if (!ofw_bus_is_compatible(dev, "chrp,open-pic") &&
106256898Snwhitehorn	    strcmp(type, "open-pic") != 0)
107256898Snwhitehorn                return (ENXIO);
108256898Snwhitehorn
109256898Snwhitehorn	/*
110256898Snwhitehorn	 * On some U4 systems, there is a phantom MPIC in the mac-io cell.
111256898Snwhitehorn	 * The uninorth driver will pick up the real PIC, so ignore it here.
112256898Snwhitehorn	 */
113242315Snwhitehorn	if (OF_finddevice("/u4") != (phandle_t)-1)
114242315Snwhitehorn		return (ENXIO);
115242315Snwhitehorn
116171805Smarcel	device_set_desc(dev, OPENPIC_DEVSTR);
117124469Sgrehan	return (0);
118124469Sgrehan}
119209298Snwhitehorn
120218075Smarcelstatic int
121256898Snwhitehornopenpic_ofw_attach(device_t dev)
122209298Snwhitehorn{
123256898Snwhitehorn	phandle_t xref, node;
124256898Snwhitehorn
125256898Snwhitehorn	node = ofw_bus_get_node(dev);
126256898Snwhitehorn
127256898Snwhitehorn	if (OF_getprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
128256898Snwhitehorn	    OF_getprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
129256898Snwhitehorn	    OF_getprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
130256898Snwhitehorn		xref = node;
131256898Snwhitehorn
132256898Snwhitehorn	return (openpic_common_attach(dev, xref));
133209298Snwhitehorn}
134256898Snwhitehorn
135265969Sianstatic void
136265969Sianopenpic_ofw_translate_code(device_t dev, u_int irq, int code,
137265969Sian    enum intr_trigger *trig, enum intr_polarity *pol)
138265969Sian{
139265969Sian	switch (code) {
140265969Sian	case 0:
141265969Sian		/* L to H edge */
142265969Sian		*trig = INTR_TRIGGER_EDGE;
143265969Sian		*pol = INTR_POLARITY_HIGH;
144265969Sian		break;
145265969Sian	case 1:
146265969Sian		/* Active L level */
147265969Sian		*trig = INTR_TRIGGER_LEVEL;
148265969Sian		*pol = INTR_POLARITY_LOW;
149265969Sian		break;
150265969Sian	case 2:
151265969Sian		/* Active H level */
152265969Sian		*trig = INTR_TRIGGER_LEVEL;
153265969Sian		*pol = INTR_POLARITY_HIGH;
154265969Sian		break;
155265969Sian	case 3:
156265969Sian		/* H to L edge */
157265969Sian		*trig = INTR_TRIGGER_EDGE;
158265969Sian		*pol = INTR_POLARITY_LOW;
159265969Sian		break;
160265969Sian	default:
161265969Sian		*trig = INTR_TRIGGER_CONFORM;
162265969Sian		*pol = INTR_POLARITY_CONFORM;
163265969Sian	}
164265969Sian}
165265969Sian
166