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