Home | History | Annotate | Line # | Download | only in pci
ali1543.c revision 1.5
      1 /*	$NetBSD: ali1543.c,v 1.5 2003/02/26 22:23:06 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001
      5  *       HAYAKAWA Koichi.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     38  * NASA Ames Research Center.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1999, by UCHIYAMA Yasushi
     71  * All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. The name of the developer may NOT be used to endorse or promote products
     79  *    derived from this software without specific prior written permission.
     80  *
     81  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     82  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     83  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     84  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     85  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     86  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     87  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     88  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     89  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     90  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     91  * SUCH DAMAGE.
     92  */
     93 
     94 /* HAYAKAWA Koichi wrote ALi 1543 PCI ICU code basing on VIA82C586 driver */
     95 
     96 #include <sys/cdefs.h>
     97 __KERNEL_RCSID(0, "$NetBSD: ali1543.c,v 1.5 2003/02/26 22:23:06 fvdl Exp $");
     98 
     99 #include <sys/param.h>
    100 #include <sys/systm.h>
    101 #include <sys/errno.h>
    102 #include <sys/device.h>
    103 #include <sys/malloc.h>
    104 #include <sys/proc.h>
    105 
    106 #include <machine/intr.h>
    107 #include <machine/bus.h>
    108 
    109 #include <dev/pci/pcivar.h>
    110 #include <dev/pci/pcireg.h>
    111 #include <dev/pci/pcidevs.h>
    112 
    113 #include <i386/pci/pci_intr_fixup.h>
    114 #include <i386/pci/piixvar.h>
    115 
    116 
    117 int ali1543_getclink (pciintr_icu_handle_t, int, int *);
    118 int ali1543_get_intr (pciintr_icu_handle_t, int, int *);
    119 int ali1543_set_intr (pciintr_icu_handle_t, int, int);
    120 
    121 
    122 const struct pciintr_icu ali1543_icu = {
    123 	ali1543_getclink,
    124 	ali1543_get_intr,
    125 	ali1543_set_intr,
    126 	piix_get_trigger,
    127 	piix_set_trigger,
    128 };
    129 
    130 
    131 /*
    132  * Linux source code (linux/arch/i386/kernel/pci-irq.c) says that the
    133  * irq order of ALi PCI ICU is shuffled.
    134  */
    135 static const int ali1543_intr_shuffle_get[16] = {
    136 	0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15
    137 };
    138 static const int ali1543_intr_shuffle_set[16] = {
    139 	0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15
    140 };
    141 
    142 #define ALI1543_IRQ_MASK		0xdefa
    143 
    144 #define ALI1543_LEGAL_LINK(link)	(((link) >= 0) && ((link) <= 7))
    145 #define ALI1543_LEGAL_IRQ(irq)		((1 << (irq)) & ALI1543_IRQ_MASK)
    146 
    147 #define ALI1543_INTR_CFG_REG		0x48
    148 
    149 #define ALI1543_INTR_PIRQA_SHIFT	0
    150 #define ALI1543_INTR_PIRQA_MASK		0x0000000f
    151 #define ALI1543_INTR_PIRQB_SHIFT	4
    152 #define ALI1543_INTR_PIRQB_MASK		0x000000f0
    153 #define ALI1543_INTR_PIRQC_SHIFT	8
    154 #define ALI1543_INTR_PIRQC_MASK		0x00000f00
    155 #define ALI1543_INTR_PIRQD_SHIFT	12
    156 #define ALI1543_INTR_PIRQD_MASK		0x0000f000
    157 
    158 #define ALI1543_INTR_PIRQ_SHIFT(clink)	((clink)*4)
    159 #define ALI1543_INTR_PIRQ_IRQ(reg, clink)				\
    160 	(((reg) >> ((clink)*4)) & 0x0f)
    161 #define ALI1543_PIRQ(reg, clink)					\
    162 	ali1543_intr_shuffle_get[ALI1543_INTR_PIRQ_IRQ((reg), (clink))]
    163 
    164 
    165 int
    166 ali1543_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
    167     pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
    168 {
    169 
    170 	if (piix_init(pc, iot, tag, ptagp, phandp) == 0) {
    171 		*ptagp = &ali1543_icu;
    172 
    173 		return (0);
    174 	}
    175 
    176 	return (1);
    177 }
    178 
    179 int
    180 ali1543_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
    181 {
    182 
    183 	if (ALI1543_LEGAL_LINK(link - 1)) {
    184 		*clinkp = link - 1;
    185 		return (0);
    186 	}
    187 
    188 	return (1);
    189 }
    190 
    191 int
    192 ali1543_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
    193 {
    194 	struct piix_handle *ph = v;
    195 	pcireg_t reg;
    196 	int val;
    197 
    198 	if (ALI1543_LEGAL_LINK(clink) == 0)
    199 		return (1);
    200 
    201 	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
    202 #ifdef DEBUG_1543
    203 	printf("ali1543: PIRQ reg 0x%08x\n", reg); /* XXX debug */
    204 #endif /* DEBUG_1543 */
    205 	val = ALI1543_PIRQ(reg, clink);
    206 	*irqp = (val == 0) ?
    207 	    X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
    208 
    209 	return (0);
    210 }
    211 
    212 int
    213 ali1543_set_intr(pciintr_icu_handle_t v, int clink, int irq)
    214 {
    215 	struct piix_handle *ph = v;
    216 	int shift, val;
    217 	pcireg_t reg;
    218 
    219 	if (ALI1543_LEGAL_LINK(clink) == 0 || ALI1543_LEGAL_IRQ(irq) == 0)
    220 		return (1);
    221 
    222 	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
    223 	ali1543_get_intr(v, clink, &val);
    224 	shift = ALI1543_INTR_PIRQ_SHIFT(clink);
    225 	reg &= ~(0x0f << shift);
    226 	reg |= (ali1543_intr_shuffle_set[irq] << shift);
    227 	pci_conf_write(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG, reg);
    228 	if (ali1543_get_intr(v, clink, &val) != 0 || val != irq)
    229 		return (1);
    230 
    231 	return (0);
    232 }
    233