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