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 DRIVER_MODULE(openpic, ofwbus, openpic_ofw_driver, openpic_devclass, 0, 0); 99 DRIVER_MODULE(openpic, simplebus, openpic_ofw_driver, openpic_devclass, 0, 0); 100 DRIVER_MODULE(openpic, macio, openpic_ofw_driver, openpic_devclass, 0, 0); 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 phandle_t xref, node; 129 130 node = ofw_bus_get_node(dev); 131 132 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 && 133 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 && 134 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1) 135 xref = node; 136 137 return (openpic_common_attach(dev, xref)); 138 } 139 140 static void 141 openpic_ofw_translate_code(device_t dev, u_int irq, int code, 142 enum intr_trigger *trig, enum intr_polarity *pol) 143 { 144 switch (code) { 145 case 0: 146 /* L to H edge */ 147 *trig = INTR_TRIGGER_EDGE; 148 *pol = INTR_POLARITY_HIGH; 149 break; 150 case 1: 151 /* Active L level */ 152 *trig = INTR_TRIGGER_LEVEL; 153 *pol = INTR_POLARITY_LOW; 154 break; 155 case 2: 156 /* Active H level */ 157 *trig = INTR_TRIGGER_LEVEL; 158 *pol = INTR_POLARITY_HIGH; 159 break; 160 case 3: 161 /* H to L edge */ 162 *trig = INTR_TRIGGER_EDGE; 163 *pol = INTR_POLARITY_LOW; 164 break; 165 default: 166 *trig = INTR_TRIGGER_CONFORM; 167 *pol = INTR_POLARITY_CONFORM; 168 } 169 } 170 171