Home | History | Annotate | Line # | Download | only in pci
pci_550.c revision 1.38
      1 /* $NetBSD: pci_550.c,v 1.38 2020/09/22 15:24:02 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 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, and by Andrew Gallatin.
     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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     35  * All rights reserved.
     36  *
     37  * Author: Chris G. Demetriou
     38  *
     39  * Permission to use, copy, modify and distribute this software and
     40  * its documentation is hereby granted, provided that both the copyright
     41  * notice and this permission notice appear in all copies of the
     42  * software, derivative works or modified versions, and any portions
     43  * thereof, and that both notices appear in supporting documentation.
     44  *
     45  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     46  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     47  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     48  *
     49  * Carnegie Mellon requests users of this software to return to
     50  *
     51  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     52  *  School of Computer Science
     53  *  Carnegie Mellon University
     54  *  Pittsburgh PA 15213-3890
     55  *
     56  * any improvements or extensions that they make and grant Carnegie the
     57  * rights to redistribute these changes.
     58  */
     59 
     60 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     61 
     62 __KERNEL_RCSID(0, "$NetBSD: pci_550.c,v 1.38 2020/09/22 15:24:02 thorpej Exp $");
     63 
     64 #include <sys/types.h>
     65 #include <sys/param.h>
     66 #include <sys/time.h>
     67 #include <sys/systm.h>
     68 #include <sys/errno.h>
     69 #include <sys/malloc.h>
     70 #include <sys/device.h>
     71 #include <sys/syslog.h>
     72 
     73 #include <machine/autoconf.h>
     74 #include <machine/rpb.h>
     75 
     76 #include <dev/pci/pcireg.h>
     77 #include <dev/pci/pcivar.h>
     78 #include <dev/pci/pciidereg.h>
     79 #include <dev/pci/pciidevar.h>
     80 
     81 #include <alpha/pci/ciareg.h>
     82 #include <alpha/pci/ciavar.h>
     83 
     84 #include <alpha/pci/pci_550.h>
     85 
     86 #include "sio.h"
     87 #if NSIO
     88 #include <alpha/pci/siovar.h>
     89 #endif
     90 
     91 static int	dec_550_intr_map(const struct pci_attach_args *,
     92 		    pci_intr_handle_t *);
     93 static const char *dec_550_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
     94 		    char *, size_t);
     95 static const struct evcnt *dec_550_intr_evcnt(pci_chipset_tag_t,
     96 		    pci_intr_handle_t);
     97 static void	*dec_550_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
     98 		    int, int (*func)(void *), void *);
     99 static void	dec_550_intr_disestablish(pci_chipset_tag_t, void *);
    100 
    101 #define	DEC_550_PCI_IRQ_BEGIN	8
    102 #define	DEC_550_MAX_IRQ		(64 - DEC_550_PCI_IRQ_BEGIN)
    103 
    104 /*
    105  * The Miata has a Pyxis, which seems to have problems with stray
    106  * interrupts.  Work around this by just ignoring strays.
    107  */
    108 #define	PCI_STRAY_MAX		0
    109 
    110 /*
    111  * Some Miata models, notably models with a Cypress PCI-ISA bridge, have
    112  * a PCI device (the OHCI USB controller) with interrupts tied to ISA IRQ
    113  * lines.  This IRQ is encoded as: line = FLAG | isa_irq. Usually FLAG
    114  * is 0xe0, however, it can be 0xf0.  We don't allow 0xf0 | irq15.
    115  */
    116 #define	DEC_550_LINE_IS_ISA(line)	((line) >= 0xe0 && (line) <= 0xfe)
    117 #define	DEC_550_LINE_ISA_IRQ(line)	((line) & 0x0f)
    118 
    119 static void	dec_550_intr_enable(pci_chipset_tag_t, int irq);
    120 static void	dec_550_intr_disable(pci_chipset_tag_t, int irq);
    121 
    122 void
    123 pci_550_pickintr(struct cia_config *ccp)
    124 {
    125 	bus_space_tag_t iot = &ccp->cc_iot;
    126 	pci_chipset_tag_t pc = &ccp->cc_pc;
    127 	char *cp;
    128 	int i;
    129 
    130 	pc->pc_intr_v = ccp;
    131 	pc->pc_intr_map = dec_550_intr_map;
    132 	pc->pc_intr_string = dec_550_intr_string;
    133 	pc->pc_intr_evcnt = dec_550_intr_evcnt;
    134 	pc->pc_intr_establish = dec_550_intr_establish;
    135 	pc->pc_intr_disestablish = dec_550_intr_disestablish;
    136 
    137 	pc->pc_pciide_compat_intr_establish =
    138 	    sio_pciide_compat_intr_establish;
    139 
    140 	/*
    141 	 * DEC 550's interrupts are enabled via the Pyxis interrupt
    142 	 * mask register.  Nothing to map.
    143 	 */
    144 
    145 	for (i = 0; i < DEC_550_MAX_IRQ; i++)
    146 		dec_550_intr_disable(pc, i);
    147 
    148 #define PCI_550_IRQ_STR	8
    149 	pc->pc_shared_intrs = alpha_shared_intr_alloc(DEC_550_MAX_IRQ,
    150 	    PCI_550_IRQ_STR);
    151 	pc->pc_intr_desc = "dec 550 irq";
    152 	pc->pc_vecbase = 0x900;
    153 	pc->pc_nirq = DEC_550_MAX_IRQ;
    154 
    155 	pc->pc_intr_enable = dec_550_intr_enable;
    156 	pc->pc_intr_disable = dec_550_intr_disable;
    157 
    158 	for (i = 0; i < DEC_550_MAX_IRQ; i++) {
    159 		alpha_shared_intr_set_maxstrays(pc->pc_shared_intrs, i,
    160 		    PCI_STRAY_MAX);
    161 		alpha_shared_intr_set_private(pc->pc_shared_intrs, i, ccp);
    162 
    163 		cp = alpha_shared_intr_string(pc->pc_shared_intrs, i);
    164 		snprintf(cp, PCI_550_IRQ_STR, "irq %d", i);
    165 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    166 		    pc->pc_shared_intrs, i), EVCNT_TYPE_INTR, NULL,
    167 		    "dec 550", cp);
    168 	}
    169 
    170 #if NSIO
    171 	sio_intr_setup(pc, iot);
    172 #endif
    173 }
    174 
    175 static int
    176 dec_550_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    177 {
    178 	pcitag_t bustag = pa->pa_intrtag;
    179 	int buspin = pa->pa_intrpin, line = pa->pa_intrline;
    180 	pci_chipset_tag_t pc = pa->pa_pc;
    181 	int bus, device, function;
    182 
    183 	if (buspin == 0) {
    184 		/* No IRQ used. */
    185 		return 1;
    186 	}
    187 	if (buspin < 0 || buspin > 4) {
    188 		printf("dec_550_intr_map: bad interrupt pin %d\n", buspin);
    189 		return 1;
    190 	}
    191 
    192 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
    193 
    194 	/*
    195 	 * There are two main variants of Miata: Miata 1 (Intel SIO)
    196 	 * and Miata {1.5,2} (Cypress).
    197 	 *
    198 	 * The Miata 1 has a CMD PCI IDE wired to compatibility mode at
    199 	 * device 4 of bus 0.  This variant apparently also has the
    200 	 * Pyxis DMA bug.
    201 	 *
    202 	 * On the Miata 1.5 and Miata 2, the Cypress PCI-ISA bridge lives
    203 	 * on device 7 of bus 0.  This device has PCI IDE wired to
    204 	 * compatibility mode on functions 1 and 2.
    205 	 *
    206 	 * There will be no interrupt mapping for these devices, so just
    207 	 * bail out now.
    208 	 */
    209 	if (bus == 0) {
    210 		if ((hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
    211 			/* Miata 1 */
    212 			if (device == 7)
    213 				panic("dec_550_intr_map: SIO device");
    214 			else if (device == 4)
    215 				return (1);
    216 		} else {
    217 			/* Miata 1.5 or Miata 2 */
    218 			if (device == 7) {
    219 				if (function == 0)
    220 					panic("dec_550_intr_map: SIO device");
    221 				if (function == 1 || function == 2)
    222 					return (1);
    223 			}
    224 		}
    225 	}
    226 
    227 	/*
    228 	 * The console places the interrupt mapping in the "line" value.
    229 	 * A value of (char)-1 indicates there is no mapping.
    230 	 */
    231 	if (line == 0xff) {
    232 		printf("dec_550_intr_map: no mapping for %d/%d/%d\n",
    233 		    bus, device, function);
    234 		return (1);
    235 	}
    236 
    237 #if NSIO == 0
    238 	if (DEC_550_LINE_IS_ISA(line)) {
    239 		printf("dec_550_intr_map: ISA IRQ %d for %d/%d/%d\n",
    240 		    DEC_550_LINE_ISA_IRQ(line), bus, device, function);
    241 		return (1);
    242 	}
    243 #endif
    244 
    245 	if (DEC_550_LINE_IS_ISA(line) == 0 && line >= DEC_550_MAX_IRQ) {
    246 		printf("dec_550_intr_map: irq %d out of range %d/%d/%d\n",
    247 		    line, bus, device, function);
    248 		return (1);
    249 	}
    250 	alpha_pci_intr_handle_init(ihp, line, 0);
    251 	return (0);
    252 }
    253 
    254 static const char *
    255 dec_550_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    256     char * const buf, size_t const len)
    257 {
    258 #if NSIO
    259 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    260 
    261 	if (DEC_550_LINE_IS_ISA(irq))
    262 		return sio_intr_string(NULL /*XXX*/,
    263 		    DEC_550_LINE_ISA_IRQ(irq), buf, len);
    264 #endif
    265 
    266 	return alpha_pci_generic_intr_string(pc, ih, buf, len);
    267 }
    268 
    269 static const struct evcnt *
    270 dec_550_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
    271 {
    272 #if NSIO
    273 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    274 
    275 	if (DEC_550_LINE_IS_ISA(irq))
    276 		return (sio_intr_evcnt(NULL /*XXX*/,
    277 		    DEC_550_LINE_ISA_IRQ(irq)));
    278 #endif
    279 
    280 	return alpha_pci_generic_intr_evcnt(pc, ih);
    281 }
    282 
    283 static void *
    284 dec_550_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    285     int const level, int (*func)(void *), void *arg)
    286 {
    287 #if NSIO
    288 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    289 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    290 
    291 	if (DEC_550_LINE_IS_ISA(irq))
    292 		return (sio_intr_establish(NULL /*XXX*/,
    293 		    DEC_550_LINE_ISA_IRQ(irq), IST_LEVEL, level, flags,
    294 		    func, arg));
    295 #endif
    296 
    297 	return alpha_pci_generic_intr_establish(pc, ih, level, func, arg);
    298 }
    299 
    300 static void
    301 dec_550_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
    302 {
    303 #if NSIO
    304 	struct alpha_shared_intrhand * const ih = cookie;
    305 
    306 	/*
    307 	 * We have to determine if this is an ISA IRQ or not!  We do this
    308 	 * by checking to see if the intrhand points back to an intrhead
    309 	 * that points to our cia_config.  If not, it's an ISA IRQ.  Pretty
    310 	 * disgusting, eh?
    311 	 */
    312 	if (ih->ih_intrhead->intr_private != pc->pc_intr_v) {
    313 		sio_intr_disestablish(NULL /*XXX*/, cookie);
    314 		return;
    315 	}
    316 #endif
    317 
    318 	alpha_pci_generic_intr_disestablish(pc, cookie);
    319 }
    320 
    321 static void
    322 dec_550_intr_enable(pci_chipset_tag_t const pc __unused, int const irq)
    323 {
    324 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 1);
    325 }
    326 
    327 static void
    328 dec_550_intr_disable(pci_chipset_tag_t const pc __unused, int const irq)
    329 {
    330 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 0);
    331 }
    332