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