1 /* $OpenBSD: via8231.c,v 1.8 2008/06/26 05:42:11 ray Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 2005, by Michael Shalayeff 34 * Copyright (c) 2003, by Matthew Gream 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 VIA Technologies Inc. VIA8231 PCI to ISA Bridge 61 * Based upon documentation: 62 * 1. VIA VT8231 South Bridge, Revision 1.85 (March 11, 2002), pg 73 63 * 2. VIA VT8237R South Bridge, Revision 2.06 (December 15, 2004), pg 100 64 * Derived from amd756.c 65 */ 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/device.h> 70 #include <sys/malloc.h> 71 72 #include <machine/intr.h> 73 #include <machine/bus.h> 74 75 #include <dev/pci/pcivar.h> 76 #include <dev/pci/pcireg.h> 77 #include <dev/pci/pcidevs.h> 78 79 #include <i386/pci/pcibiosvar.h> 80 #include <i386/pci/via8231reg.h> 81 82 struct via8231_handle { 83 bus_space_tag_t ph_iot; 84 bus_space_handle_t ph_regs_ioh; 85 pci_chipset_tag_t ph_pc; 86 pcitag_t ph_tag; 87 int flags; 88 #define VT8237 0x0001 89 }; 90 91 int via8231_getclink(pciintr_icu_handle_t, int, int *); 92 int via8231_get_intr(pciintr_icu_handle_t, int, int *); 93 int via8231_set_intr(pciintr_icu_handle_t, int, int); 94 int via8231_get_trigger(pciintr_icu_handle_t, int, int *); 95 int via8231_set_trigger(pciintr_icu_handle_t, int, int); 96 #ifdef VIA8231_DEBUG 97 static void via8231_pir_dump(const char*, struct via8231_handle *); 98 #endif 99 100 const struct pciintr_icu via8231_pci_icu = { 101 via8231_getclink, 102 via8231_get_intr, 103 via8231_set_intr, 104 via8231_get_trigger, 105 via8231_set_trigger, 106 }; 107 108 struct mask_shft_pair { 109 int mask; 110 int shft; 111 }; 112 113 static const struct mask_shft_pair via8231_routing_cnfg[VIA8231_LINK_MAX+1] = { 114 { 0x0f, 0+4 }, /*PINTA#*/ 115 { 0x0f, 8+0 }, /*PINTB#*/ 116 { 0x0f, 8+4 }, /*PINTC#*/ 117 { 0x0f, 16+4 } /*PINTD#*/ 118 }; 119 120 #define VIA8231_GET_TRIGGER_CNFG(reg, pirq) \ 121 ((reg) & (1 << (3 - (clink & 3)))) 122 #define VIA8231_SET_TRIGGER_CNFG(reg, clink, cfg) \ 123 (((reg) & ~(1 << (3 - (clink & 3)))) | ((cfg) << (3 - (clink & 3)))) 124 125 #define VIA8231_GET_ROUTING_CNFG(reg, pirq) \ 126 (((reg) >> via8231_routing_cnfg[(pirq)].shft) & \ 127 via8231_routing_cnfg[(pirq)].mask) 128 129 #define VIA8231_SET_ROUTING_CNFG(reg, pirq, cfg) \ 130 (((reg) & ~(via8231_routing_cnfg[(pirq)].mask << \ 131 via8231_routing_cnfg[(pirq)].shft)) | \ 132 (((cfg) & via8231_routing_cnfg[(pirq)].mask) << \ 133 via8231_routing_cnfg[(pirq)].shft)) 134 135 int 136 via8231_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag, 137 pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp) 138 { 139 struct via8231_handle *ph; 140 pcireg_t id; 141 142 ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT); 143 if (ph == NULL) 144 return (1); 145 146 ph->ph_iot = iot; 147 ph->ph_pc = pc; 148 ph->ph_tag = tag; 149 id = pci_conf_read(pc, tag, PCI_ID_REG); 150 ph->flags = PCI_VENDOR(id) == PCI_VENDOR_VIATECH && 151 PCI_PRODUCT(id) == PCI_PRODUCT_VIATECH_VT8231_ISA? 0 : VT8237; 152 153 *ptagp = &via8231_pci_icu; 154 *phandp = ph; 155 156 #ifdef VIA8231_DEBUG 157 via8231_pir_dump("via8231_init", ph); 158 #endif 159 160 return 0; 161 } 162 163 int 164 via8231_getclink(pciintr_icu_handle_t v, int link, int *clinkp) 165 { 166 struct via8231_handle *ph = v; 167 168 if ((ph->flags & VT8237) && !VIA8237_LINK_LEGAL(link - 1)) 169 return (1); 170 171 if (!(ph->flags & VT8237) && !VIA8231_LINK_LEGAL(link - 1)) 172 return (1); 173 174 *clinkp = link - 1; 175 return (0); 176 } 177 178 int 179 via8231_get_intr(pciintr_icu_handle_t v, int clink, int *irqp) 180 { 181 struct via8231_handle *ph = v; 182 int reg, val; 183 184 if (VIA8237_LINK_LEGAL(clink) == 0) 185 return (1); 186 187 if (VIA8231_LINK_LEGAL(clink)) { 188 reg = VIA8231_GET_ROUTING(ph); 189 val = VIA8231_GET_ROUTING_CNFG(reg, clink); 190 } else { 191 reg = VIA8237_GET_ROUTING(ph); 192 val = (reg >> ((clink & 3) * 4)) & 0xf; 193 } 194 195 *irqp = (val == VIA8231_ROUTING_CNFG_DISABLED) ? 196 I386_PCI_INTERRUPT_LINE_NO_CONNECTION : val; 197 198 return (0); 199 } 200 201 int 202 via8231_set_intr(pciintr_icu_handle_t v, int clink, int irq) 203 { 204 struct via8231_handle *ph = v; 205 int reg; 206 207 if (VIA8237_LINK_LEGAL(clink) == 0 || VIA8231_PIRQ_LEGAL(irq) == 0) 208 return (1); 209 210 #ifdef VIA8231_DEBUG 211 printf("via8231_set_intr: link(%02x) --> irq(%02x)\n", clink, irq); 212 via8231_pir_dump("via8231_set_intr: ", ph); 213 #endif 214 215 if (VIA8231_LINK_LEGAL(clink)) { 216 reg = VIA8231_GET_ROUTING(ph); 217 VIA8231_SET_ROUTING(ph, 218 VIA8231_SET_ROUTING_CNFG(reg, clink, irq)); 219 } else { 220 reg = VIA8237_GET_ROUTING(ph); 221 VIA8237_SET_ROUTING(ph, (reg & ~(0xf << (clink & 3))) | 222 ((irq & 0xf) << (clink & 3))); 223 } 224 225 return (0); 226 } 227 228 int 229 via8231_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp) 230 { 231 struct via8231_handle *ph = v; 232 int reg, clink, max, pciirq; 233 234 if (VIA8231_PIRQ_LEGAL(irq) == 0) 235 return (1); 236 237 max = ph->flags & VT8237? VIA8237_LINK_MAX : VIA8231_LINK_MAX; 238 for (clink = 0; clink <= max; clink++) { 239 via8231_get_intr(v, clink, &pciirq); 240 if (pciirq == irq) { 241 reg = VIA8231_LINK_LEGAL(clink)? 242 VIA8231_GET_TRIGGER(ph): 243 VIA8237_GET_TRIGGER(ph); 244 *triggerp = VIA8231_GET_TRIGGER_CNFG(reg, clink)? 245 IST_EDGE : IST_LEVEL; 246 return (0); 247 } 248 } 249 250 return (1); 251 } 252 253 int 254 via8231_set_trigger(pciintr_icu_handle_t v, int irq, int trigger) 255 { 256 struct via8231_handle *ph = v; 257 int reg, clink, max, pciirq; 258 259 if (VIA8231_PIRQ_LEGAL(irq) == 0 || VIA8231_TRIG_LEGAL(trigger) == 0) 260 return (1); 261 262 #ifdef VIA8231_DEBUG 263 printf("via8231_set_trig: irq(%02x) --> trig(%02x)\n", irq, trigger); 264 via8231_pir_dump("via8231_set_trig: ", ph); 265 #endif 266 267 max = ph->flags & VT8237? VIA8237_LINK_MAX : VIA8231_LINK_MAX; 268 for (clink = 0; clink <= VIA8231_LINK_MAX; clink++) { 269 via8231_get_intr(v, clink, &pciirq); 270 if (pciirq == irq) { 271 reg = VIA8231_LINK_LEGAL(clink)? 272 VIA8231_GET_TRIGGER(ph): 273 VIA8237_GET_TRIGGER(ph); 274 switch (trigger) { 275 case IST_LEVEL: 276 reg = VIA8231_SET_TRIGGER_CNFG(reg, clink, 277 VIA8231_TRIGGER_CNFG_LEVEL); 278 break; 279 case IST_EDGE: 280 reg = VIA8231_SET_TRIGGER_CNFG(reg, clink, 281 VIA8231_TRIGGER_CNFG_EDGE); 282 break; 283 default: 284 return (1); 285 } 286 if (VIA8231_LINK_LEGAL(clink)) 287 VIA8231_SET_TRIGGER(ph, reg); 288 else 289 VIA8237_SET_TRIGGER(ph, reg); 290 return (0); 291 } 292 } 293 294 return (1); 295 } 296 297 #ifdef VIA8231_DEBUG 298 static void 299 via8231_pir_dump(const char *m, struct via8231_handle *ph) 300 { 301 int a, b; 302 303 a = VIA8231_GET_TRIGGER(ph); 304 b = VIA8231_GET_ROUTING(ph); 305 306 printf("%s STATE: trigger(%02x), routing(%08x)\n", m, a, b); 307 } 308 #endif 309