1 /* $OpenBSD: opti82c558.c,v 1.8 2008/06/26 05:42:11 ray Exp $ */ 2 /* $NetBSD: opti82c558.c,v 1.2 2000/07/18 11:24:09 soda Exp $ */ 3 4 /*- 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1999, by UCHIYAMA Yasushi 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. The name of the developer may NOT be used to endorse or promote products 44 * derived from this software without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 */ 58 59 /* 60 * Support for the Opti 82c558 PCI-ISA bridge interrupt controller. 61 */ 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/device.h> 66 #include <sys/malloc.h> 67 68 #include <machine/intr.h> 69 #include <machine/bus.h> 70 71 #include <dev/pci/pcivar.h> 72 #include <dev/pci/pcireg.h> 73 #include <dev/pci/pcidevs.h> 74 75 #include <i386/pci/pcibiosvar.h> 76 #include <i386/pci/opti82c558reg.h> 77 78 int opti82c558_getclink(pciintr_icu_handle_t, int, int *); 79 int opti82c558_get_intr(pciintr_icu_handle_t, int, int *); 80 int opti82c558_set_intr(pciintr_icu_handle_t, int, int); 81 int opti82c558_get_trigger(pciintr_icu_handle_t, int, int *); 82 int opti82c558_set_trigger(pciintr_icu_handle_t, int, int); 83 84 const struct pciintr_icu opti82c558_pci_icu = { 85 opti82c558_getclink, 86 opti82c558_get_intr, 87 opti82c558_set_intr, 88 opti82c558_get_trigger, 89 opti82c558_set_trigger, 90 }; 91 92 struct opti82c558_handle { 93 pci_chipset_tag_t ph_pc; 94 pcitag_t ph_tag; 95 }; 96 97 static const int viper_pirq_decode[] = { 98 -1, 5, 9, 10, 11, 12, 14, 15 99 }; 100 101 static const int viper_pirq_encode[] = { 102 -1, /* 0 */ 103 -1, /* 1 */ 104 -1, /* 2 */ 105 -1, /* 3 */ 106 -1, /* 4 */ 107 VIPER_PIRQ_5, /* 5 */ 108 -1, /* 6 */ 109 -1, /* 7 */ 110 -1, /* 8 */ 111 VIPER_PIRQ_9, /* 9 */ 112 VIPER_PIRQ_10, /* 10 */ 113 VIPER_PIRQ_11, /* 11 */ 114 VIPER_PIRQ_12, /* 12 */ 115 -1, /* 13 */ 116 VIPER_PIRQ_14, /* 14 */ 117 VIPER_PIRQ_15, /* 15 */ 118 }; 119 120 int 121 opti82c558_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag, 122 pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp) 123 { 124 struct opti82c558_handle *ph; 125 126 ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT); 127 if (ph == NULL) 128 return (1); 129 130 ph->ph_pc = pc; 131 ph->ph_tag = tag; 132 133 *ptagp = &opti82c558_pci_icu; 134 *phandp = ph; 135 return (0); 136 } 137 138 int 139 opti82c558_getclink(pciintr_icu_handle_t v, int link, int *clinkp) 140 { 141 142 if (VIPER_LEGAL_LINK(link - 1)) { 143 *clinkp = link - 1; 144 return (0); 145 } 146 147 return (1); 148 } 149 150 int 151 opti82c558_get_intr(pciintr_icu_handle_t v, int clink, int *irqp) 152 { 153 struct opti82c558_handle *ph = v; 154 pcireg_t reg; 155 int val; 156 157 if (VIPER_LEGAL_LINK(clink) == 0) 158 return (1); 159 160 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ); 161 val = VIPER_PIRQ(reg, clink); 162 *irqp = (val == VIPER_PIRQ_NONE) ? 0xff : viper_pirq_decode[val]; 163 164 return (0); 165 } 166 167 int 168 opti82c558_set_intr(pciintr_icu_handle_t v, int clink, int irq) 169 { 170 struct opti82c558_handle *ph = v; 171 int shift; 172 pcireg_t reg; 173 174 if (VIPER_LEGAL_LINK(clink) == 0 || VIPER_LEGAL_IRQ(irq) == 0) 175 return (1); 176 177 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ); 178 shift = VIPER_PIRQ_SELECT_SHIFT * clink; 179 reg &= ~(VIPER_PIRQ_SELECT_MASK << shift); 180 reg |= (viper_pirq_encode[irq] << shift); 181 pci_conf_write(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ, reg); 182 183 return (0); 184 } 185 186 int 187 opti82c558_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp) 188 { 189 struct opti82c558_handle *ph = v; 190 pcireg_t reg; 191 192 if (VIPER_LEGAL_IRQ(irq) == 0) { 193 /* ISA IRQ? */ 194 *triggerp = IST_EDGE; 195 return (0); 196 } 197 198 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ); 199 if ((reg >> (VIPER_CFG_TRIGGER_SHIFT + viper_pirq_encode[irq])) & 1) 200 *triggerp = IST_LEVEL; 201 else 202 *triggerp = IST_EDGE; 203 204 return (0); 205 } 206 207 int 208 opti82c558_set_trigger(pciintr_icu_handle_t v, int irq, int trigger) 209 { 210 struct opti82c558_handle *ph = v; 211 int shift; 212 pcireg_t reg; 213 214 if (VIPER_LEGAL_IRQ(irq) == 0) { 215 /* ISA IRQ? */ 216 return ((trigger != IST_LEVEL) ? 0 : 1); 217 } 218 219 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ); 220 shift = (VIPER_CFG_TRIGGER_SHIFT + viper_pirq_encode[irq]); 221 if (trigger == IST_LEVEL) 222 reg |= (1 << shift); 223 else 224 reg &= ~(1 << shift); 225 pci_conf_write(ph->ph_pc, ph->ph_tag, VIPER_CFG_PIRQ, reg); 226 227 return (0); 228 } 229