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