Home | History | Annotate | Line # | Download | only in pci
via82c586.c revision 1.6
      1 /*	$NetBSD: via82c586.c,v 1.6 2004/04/11 06:00:26 kochi 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1999, by UCHIYAMA Yasushi
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. The name of the developer may NOT be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * Support for the VIA 82c586 PCI-ISA bridge interrupt controller.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: via82c586.c,v 1.6 2004/04/11 06:00:26 kochi Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/device.h>
     75 
     76 #include <machine/intr.h>
     77 #include <machine/bus.h>
     78 
     79 #include <dev/pci/pcivar.h>
     80 #include <dev/pci/pcireg.h>
     81 #include <dev/pci/pcidevs.h>
     82 
     83 #include <i386/pci/pci_intr_fixup.h>
     84 #include <i386/pci/via82c586reg.h>
     85 #include <i386/pci/piixvar.h>
     86 
     87 int	via82c586_getclink(pciintr_icu_handle_t, int, int *);
     88 int	via82c586_get_intr(pciintr_icu_handle_t, int, int *);
     89 int	via82c586_set_intr(pciintr_icu_handle_t, int, int);
     90 int	via82c586_get_trigger(pciintr_icu_handle_t, int, int *);
     91 int	via82c586_set_trigger(pciintr_icu_handle_t, int, int);
     92 
     93 const struct pciintr_icu via82c586_pci_icu = {
     94 	via82c586_getclink,
     95 	via82c586_get_intr,
     96 	via82c586_set_intr,
     97 	via82c586_get_trigger,
     98 	via82c586_set_trigger,
     99 };
    100 
    101 const int vp3_cfg_trigger_shift[] = {
    102 	VP3_CFG_TRIGGER_SHIFT_PIRQA,
    103 	VP3_CFG_TRIGGER_SHIFT_PIRQB,
    104 	VP3_CFG_TRIGGER_SHIFT_PIRQC,
    105 	VP3_CFG_TRIGGER_SHIFT_PIRQD,
    106 };
    107 
    108 #define	VP3_TRIGGER(reg, pirq)	(((reg) >> vp3_cfg_trigger_shift[(pirq)]) & \
    109 				 VP3_CFG_TRIGGER_MASK)
    110 
    111 const int vp3_cfg_intr_shift[] = {
    112 	VP3_CFG_INTR_SHIFT_PIRQA,
    113 	VP3_CFG_INTR_SHIFT_PIRQB,
    114 	VP3_CFG_INTR_SHIFT_PIRQC,
    115 	VP3_CFG_INTR_SHIFT_PIRQD,
    116 };
    117 
    118 #define	VP3_PIRQ(reg, pirq)	(((reg) >> vp3_cfg_intr_shift[(pirq)]) & \
    119 				 VP3_CFG_INTR_MASK)
    120 
    121 int
    122 via82c586_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
    123     pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
    124 {
    125 	pcireg_t reg;
    126 
    127 	if (piix_init(pc, iot, tag, ptagp, phandp) == 0) {
    128 		*ptagp = &via82c586_pci_icu;
    129 
    130 		/*
    131 		 * Enable EISA ELCR.
    132 		 */
    133 		reg = pci_conf_read(pc, tag, VP3_CFG_KBDMISCCTRL12_REG);
    134 		reg |= VP3_CFG_MISCCTRL2_EISA4D04D1PORT_ENABLE <<
    135 		    VP3_CFG_MISCCTRL2_SHIFT;
    136 		pci_conf_write(pc, tag, VP3_CFG_KBDMISCCTRL12_REG, reg);
    137 
    138 		return (0);
    139 	}
    140 
    141 	return (1);
    142 }
    143 
    144 int
    145 via82c586_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
    146 {
    147 
    148 	if (VP3_LEGAL_LINK(link - 1)) {
    149 		*clinkp = link - 1;
    150 		return (0);
    151 	}
    152 
    153 	return (1);
    154 }
    155 
    156 int
    157 via82c586_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
    158 {
    159 	struct piix_handle *ph = v;
    160 	pcireg_t reg;
    161 	int val;
    162 
    163 	if (VP3_LEGAL_LINK(clink) == 0)
    164 		return (1);
    165 
    166 	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VP3_CFG_PIRQ_REG);
    167 	val = VP3_PIRQ(reg, clink);
    168 	*irqp = (val == VP3_PIRQ_NONE) ?
    169 	    X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
    170 
    171 	return (0);
    172 }
    173 
    174 int
    175 via82c586_set_intr(pciintr_icu_handle_t v, int clink, int irq)
    176 {
    177 	struct piix_handle *ph = v;
    178 	int shift, val;
    179 	pcireg_t reg;
    180 
    181 	if (VP3_LEGAL_LINK(clink) == 0 || VP3_LEGAL_IRQ(irq) == 0)
    182 		return (1);
    183 
    184 	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, VP3_CFG_PIRQ_REG);
    185 	via82c586_get_intr(v, clink, &val);
    186 	shift = vp3_cfg_intr_shift[clink];
    187 	reg &= ~(VP3_CFG_INTR_MASK << shift);
    188 	reg |= (irq << shift);
    189 	pci_conf_write(ph->ph_pc, ph->ph_tag, VP3_CFG_PIRQ_REG, reg);
    190 	if (via82c586_get_intr(v, clink, &val) != 0 ||
    191 	    val != irq)
    192 		return (1);
    193 
    194 	return (0);
    195 }
    196 
    197 int
    198 via82c586_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
    199 {
    200 	struct piix_handle *ph = v;
    201 	int i, error, check_consistency, pciirq, pcitrigger = IST_NONE;
    202 	pcireg_t reg;
    203 
    204 	if (VP3_LEGAL_IRQ(irq) == 0)
    205 		return (1);
    206 
    207 	check_consistency = 0;
    208 	for (i = 0; i <= 3; i++) {
    209 		via82c586_get_intr(v, i, &pciirq);
    210 		if (pciirq == irq) {
    211 			reg = pci_conf_read(ph->ph_pc, ph->ph_tag,
    212 			    VP3_CFG_PIRQ_REG);
    213 			if (VP3_TRIGGER(reg, i) == VP3_CFG_TRIGGER_EDGE)
    214 				pcitrigger = IST_EDGE;
    215 			else
    216 				pcitrigger = IST_LEVEL;
    217 			check_consistency = 1;
    218 			break;
    219 		}
    220 	}
    221 
    222 	error = piix_get_trigger(v, irq, triggerp);
    223 	if (error == 0 && check_consistency && pcitrigger != *triggerp)
    224 		return (1);
    225 	return (error);
    226 }
    227 
    228 int
    229 via82c586_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
    230 {
    231 	struct piix_handle *ph = v;
    232 	int i, pciirq, shift, testtrig;
    233 	pcireg_t reg;
    234 
    235 	if (VP3_LEGAL_IRQ(irq) == 0)
    236 		return (1);
    237 
    238 	for (i = 0; i <= 3; i++) {
    239 		via82c586_get_intr(v, i, &pciirq);
    240 		if (pciirq == irq) {
    241 			reg = pci_conf_read(ph->ph_pc, ph->ph_tag,
    242 			    VP3_CFG_PIRQ_REG);
    243 			shift = vp3_cfg_trigger_shift[i];
    244 			if (trigger == IST_LEVEL)
    245 				reg &= ~(VP3_CFG_TRIGGER_MASK << shift);
    246 			else
    247 				reg |= (VP3_CFG_TRIGGER_EDGE << shift);
    248 			pci_conf_write(ph->ph_pc, ph->ph_tag,
    249 			    VP3_CFG_PIRQ_REG, reg);
    250 			break;
    251 		}
    252 	}
    253 
    254 	if (piix_set_trigger(v, irq, trigger) != 0 ||
    255 	    via82c586_get_trigger(v, irq, &testtrig) != 0 ||
    256 	    testtrig != trigger)
    257 		return (1);
    258 
    259 	return (0);
    260 }
    261