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: releng/11.0/sys/powerpc/ofw/openpic_ofw.c 290989 2015-11-17 16:07:43Z nwhitehorn $");
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
54259676Sjhibbits#include <machine/openpicreg.h>
55124469Sgrehan#include <machine/openpicvar.h>
56124469Sgrehan
57124469Sgrehan#include "pic_if.h"
58124469Sgrehan
59124469Sgrehan/*
60256898Snwhitehorn * OFW interface
61124469Sgrehan */
62256898Snwhitehornstatic int	openpic_ofw_probe(device_t);
63256898Snwhitehornstatic int	openpic_ofw_attach(device_t);
64124469Sgrehan
65257059Snwhitehornstatic void	openpic_ofw_translate_code(device_t, u_int irq, int code,
66257059Snwhitehorn		    enum intr_trigger *trig, enum intr_polarity *pol);
67257059Snwhitehorn
68256898Snwhitehornstatic device_method_t  openpic_ofw_methods[] = {
69124469Sgrehan	/* Device interface */
70256898Snwhitehorn	DEVMETHOD(device_probe,		openpic_ofw_probe),
71256898Snwhitehorn	DEVMETHOD(device_attach,	openpic_ofw_attach),
72259676Sjhibbits	DEVMETHOD(device_suspend,	openpic_suspend),
73259676Sjhibbits	DEVMETHOD(device_resume,	openpic_resume),
74124469Sgrehan
75124469Sgrehan	/* PIC interface */
76209486Snwhitehorn	DEVMETHOD(pic_bind,		openpic_bind),
77176918Smarcel	DEVMETHOD(pic_config,		openpic_config),
78171805Smarcel	DEVMETHOD(pic_dispatch,		openpic_dispatch),
79171805Smarcel	DEVMETHOD(pic_enable,		openpic_enable),
80171805Smarcel	DEVMETHOD(pic_eoi,		openpic_eoi),
81176208Smarcel	DEVMETHOD(pic_ipi,		openpic_ipi),
82171805Smarcel	DEVMETHOD(pic_mask,		openpic_mask),
83171805Smarcel	DEVMETHOD(pic_unmask,		openpic_unmask),
84124469Sgrehan
85257059Snwhitehorn	DEVMETHOD(pic_translate_code,	openpic_ofw_translate_code),
86257059Snwhitehorn
87256898Snwhitehorn	DEVMETHOD_END
88124469Sgrehan};
89124469Sgrehan
90256898Snwhitehornstatic driver_t openpic_ofw_driver = {
91171805Smarcel	"openpic",
92256898Snwhitehorn	openpic_ofw_methods,
93171805Smarcel	sizeof(struct openpic_softc),
94124469Sgrehan};
95124469Sgrehan
96261513SnwhitehornDRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, openpic_devclass, 0, 0);
97256898SnwhitehornDRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, openpic_devclass, 0, 0);
98256898SnwhitehornDRIVER_MODULE(openpic, macio, openpic_ofw_driver, openpic_devclass, 0, 0);
99124469Sgrehan
100124469Sgrehanstatic int
101256898Snwhitehornopenpic_ofw_probe(device_t dev)
102124469Sgrehan{
103133589Smarius	const char *type = ofw_bus_get_type(dev);
104124469Sgrehan
105256898Snwhitehorn	if (type == NULL)
106124469Sgrehan                return (ENXIO);
107124469Sgrehan
108256898Snwhitehorn	if (!ofw_bus_is_compatible(dev, "chrp,open-pic") &&
109256898Snwhitehorn	    strcmp(type, "open-pic") != 0)
110256898Snwhitehorn                return (ENXIO);
111256898Snwhitehorn
112256898Snwhitehorn	/*
113256898Snwhitehorn	 * On some U4 systems, there is a phantom MPIC in the mac-io cell.
114256898Snwhitehorn	 * The uninorth driver will pick up the real PIC, so ignore it here.
115256898Snwhitehorn	 */
116242315Snwhitehorn	if (OF_finddevice("/u4") != (phandle_t)-1)
117242315Snwhitehorn		return (ENXIO);
118242315Snwhitehorn
119171805Smarcel	device_set_desc(dev, OPENPIC_DEVSTR);
120124469Sgrehan	return (0);
121124469Sgrehan}
122209298Snwhitehorn
123218075Smarcelstatic int
124256898Snwhitehornopenpic_ofw_attach(device_t dev)
125209298Snwhitehorn{
126256898Snwhitehorn	phandle_t xref, node;
127256898Snwhitehorn
128256898Snwhitehorn	node = ofw_bus_get_node(dev);
129256898Snwhitehorn
130290989Snwhitehorn	if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
131290989Snwhitehorn	    OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
132290989Snwhitehorn	    OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
133256898Snwhitehorn		xref = node;
134256898Snwhitehorn
135256898Snwhitehorn	return (openpic_common_attach(dev, xref));
136209298Snwhitehorn}
137256898Snwhitehorn
138257059Snwhitehornstatic void
139257059Snwhitehornopenpic_ofw_translate_code(device_t dev, u_int irq, int code,
140257059Snwhitehorn    enum intr_trigger *trig, enum intr_polarity *pol)
141257059Snwhitehorn{
142257059Snwhitehorn	switch (code) {
143257059Snwhitehorn	case 0:
144257059Snwhitehorn		/* L to H edge */
145257059Snwhitehorn		*trig = INTR_TRIGGER_EDGE;
146257059Snwhitehorn		*pol = INTR_POLARITY_HIGH;
147257059Snwhitehorn		break;
148257059Snwhitehorn	case 1:
149257059Snwhitehorn		/* Active L level */
150257059Snwhitehorn		*trig = INTR_TRIGGER_LEVEL;
151257059Snwhitehorn		*pol = INTR_POLARITY_LOW;
152257059Snwhitehorn		break;
153257059Snwhitehorn	case 2:
154257059Snwhitehorn		/* Active H level */
155257059Snwhitehorn		*trig = INTR_TRIGGER_LEVEL;
156257059Snwhitehorn		*pol = INTR_POLARITY_HIGH;
157257059Snwhitehorn		break;
158257059Snwhitehorn	case 3:
159257059Snwhitehorn		/* H to L edge */
160257059Snwhitehorn		*trig = INTR_TRIGGER_EDGE;
161257059Snwhitehorn		*pol = INTR_POLARITY_LOW;
162257059Snwhitehorn		break;
163257059Snwhitehorn	default:
164257059Snwhitehorn		*trig = INTR_TRIGGER_CONFORM;
165257059Snwhitehorn		*pol = INTR_POLARITY_CONFORM;
166257059Snwhitehorn	}
167257059Snwhitehorn}
168257059Snwhitehorn
169