Home | History | Annotate | Line # | Download | only in pci
pci_550.c revision 1.41
      1 /* $NetBSD: pci_550.c,v 1.41 2021/07/04 22:36:43 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.41 2021/07/04 22:36:43 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 "sio.h"
     85 #if NSIO
     86 #include <alpha/pci/siovar.h>
     87 #endif
     88 
     89 static int	dec_550_intr_map(const struct pci_attach_args *,
     90 		    pci_intr_handle_t *);
     91 static const char *dec_550_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
     92 		    char *, size_t);
     93 static const struct evcnt *dec_550_intr_evcnt(pci_chipset_tag_t,
     94 		    pci_intr_handle_t);
     95 static void	*dec_550_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
     96 		    int, int (*func)(void *), void *);
     97 static void	dec_550_intr_disestablish(pci_chipset_tag_t, void *);
     98 
     99 #define	DEC_550_PCI_IRQ_BEGIN	8
    100 #define	DEC_550_MAX_IRQ		(64 - DEC_550_PCI_IRQ_BEGIN)
    101 
    102 /*
    103  * The Miata has a Pyxis, which seems to have problems with stray
    104  * interrupts.  Work around this by just ignoring strays.
    105  */
    106 #define	PCI_STRAY_MAX		0
    107 
    108 /*
    109  * Some Miata models, notably models with a Cypress PCI-ISA bridge, have
    110  * a PCI device (the OHCI USB controller) with interrupts tied to ISA IRQ
    111  * lines.  This IRQ is encoded as: line = FLAG | isa_irq. Usually FLAG
    112  * is 0xe0, however, it can be 0xf0.  We don't allow 0xf0 | irq15.
    113  */
    114 #define	DEC_550_LINE_IS_ISA(line)	((line) >= 0xe0 && (line) <= 0xfe)
    115 #define	DEC_550_LINE_ISA_IRQ(line)	((line) & 0x0f)
    116 
    117 static void	dec_550_intr_enable(pci_chipset_tag_t, int irq);
    118 static void	dec_550_intr_disable(pci_chipset_tag_t, int irq);
    119 
    120 static void
    121 pci_550_pickintr(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
    122     pci_chipset_tag_t pc)
    123 {
    124 	int i;
    125 
    126 	pc->pc_intr_v = core;
    127 	pc->pc_intr_map = dec_550_intr_map;
    128 	pc->pc_intr_string = dec_550_intr_string;
    129 	pc->pc_intr_evcnt = dec_550_intr_evcnt;
    130 	pc->pc_intr_establish = dec_550_intr_establish;
    131 	pc->pc_intr_disestablish = dec_550_intr_disestablish;
    132 
    133 	pc->pc_pciide_compat_intr_establish =
    134 	    sio_pciide_compat_intr_establish;
    135 
    136 	pc->pc_intr_desc = "dec 550";
    137 	pc->pc_vecbase = 0x900;
    138 	pc->pc_nirq = DEC_550_MAX_IRQ;
    139 
    140 	pc->pc_intr_enable = dec_550_intr_enable;
    141 	pc->pc_intr_disable = dec_550_intr_disable;
    142 
    143 	for (i = 0; i < DEC_550_MAX_IRQ; i++) {
    144 		dec_550_intr_disable(pc, i);
    145 	}
    146 
    147 	alpha_pci_intr_alloc(pc, PCI_STRAY_MAX);
    148 
    149 #if NSIO
    150 	sio_intr_setup(pc, iot);
    151 #endif
    152 }
    153 ALPHA_PCI_INTR_INIT(ST_DEC_550, pci_550_pickintr)
    154 
    155 static int
    156 dec_550_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    157 {
    158 	pcitag_t bustag = pa->pa_intrtag;
    159 	int buspin = pa->pa_intrpin, line = pa->pa_intrline;
    160 	pci_chipset_tag_t pc = pa->pa_pc;
    161 	int bus, device, function;
    162 
    163 	if (buspin == 0) {
    164 		/* No IRQ used. */
    165 		return 1;
    166 	}
    167 	if (buspin < 0 || buspin > 4) {
    168 		printf("dec_550_intr_map: bad interrupt pin %d\n", buspin);
    169 		return 1;
    170 	}
    171 
    172 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
    173 
    174 	/*
    175 	 * There are two main variants of Miata: Miata 1 (Intel SIO)
    176 	 * and Miata {1.5,2} (Cypress).
    177 	 *
    178 	 * The Miata 1 has a CMD PCI IDE wired to compatibility mode at
    179 	 * device 4 of bus 0.  This variant apparently also has the
    180 	 * Pyxis DMA bug.
    181 	 *
    182 	 * On the Miata 1.5 and Miata 2, the Cypress PCI-ISA bridge lives
    183 	 * on device 7 of bus 0.  This device has PCI IDE wired to
    184 	 * compatibility mode on functions 1 and 2.
    185 	 *
    186 	 * There will be no interrupt mapping for these devices, so just
    187 	 * bail out now.
    188 	 */
    189 	if (bus == 0) {
    190 		if ((hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
    191 			/* Miata 1 */
    192 			if (device == 7)
    193 				panic("dec_550_intr_map: SIO device");
    194 			else if (device == 4)
    195 				return (1);
    196 		} else {
    197 			/* Miata 1.5 or Miata 2 */
    198 			if (device == 7) {
    199 				if (function == 0)
    200 					panic("dec_550_intr_map: SIO device");
    201 				if (function == 1 || function == 2)
    202 					return (1);
    203 			}
    204 		}
    205 	}
    206 
    207 	/*
    208 	 * The console places the interrupt mapping in the "line" value.
    209 	 * A value of (char)-1 indicates there is no mapping.
    210 	 */
    211 	if (line == 0xff) {
    212 		printf("dec_550_intr_map: no mapping for %d/%d/%d\n",
    213 		    bus, device, function);
    214 		return (1);
    215 	}
    216 
    217 #if NSIO == 0
    218 	if (DEC_550_LINE_IS_ISA(line)) {
    219 		printf("dec_550_intr_map: ISA IRQ %d for %d/%d/%d\n",
    220 		    DEC_550_LINE_ISA_IRQ(line), bus, device, function);
    221 		return (1);
    222 	}
    223 #endif
    224 
    225 	if (DEC_550_LINE_IS_ISA(line) == 0 && line >= DEC_550_MAX_IRQ) {
    226 		printf("dec_550_intr_map: irq %d out of range %d/%d/%d\n",
    227 		    line, bus, device, function);
    228 		return (1);
    229 	}
    230 	alpha_pci_intr_handle_init(ihp, line, 0);
    231 	return (0);
    232 }
    233 
    234 static const char *
    235 dec_550_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    236     char * const buf, size_t const len)
    237 {
    238 #if NSIO
    239 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    240 
    241 	if (DEC_550_LINE_IS_ISA(irq))
    242 		return sio_intr_string(NULL /*XXX*/,
    243 		    DEC_550_LINE_ISA_IRQ(irq), buf, len);
    244 #endif
    245 
    246 	return alpha_pci_generic_intr_string(pc, ih, buf, len);
    247 }
    248 
    249 static const struct evcnt *
    250 dec_550_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
    251 {
    252 #if NSIO
    253 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    254 
    255 	if (DEC_550_LINE_IS_ISA(irq))
    256 		return (sio_intr_evcnt(NULL /*XXX*/,
    257 		    DEC_550_LINE_ISA_IRQ(irq)));
    258 #endif
    259 
    260 	return alpha_pci_generic_intr_evcnt(pc, ih);
    261 }
    262 
    263 static void *
    264 dec_550_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    265     int const level, int (*func)(void *), void *arg)
    266 {
    267 #if NSIO
    268 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    269 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    270 
    271 	if (DEC_550_LINE_IS_ISA(irq))
    272 		return (sio_intr_establish(NULL /*XXX*/,
    273 		    DEC_550_LINE_ISA_IRQ(irq), IST_LEVEL, level, flags,
    274 		    func, arg));
    275 #endif
    276 
    277 	return alpha_pci_generic_intr_establish(pc, ih, level, func, arg);
    278 }
    279 
    280 static void
    281 dec_550_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
    282 {
    283 #if NSIO
    284 	struct alpha_shared_intrhand * const ih = cookie;
    285 
    286 	/*
    287 	 * We have to determine if this is an ISA IRQ or not!  We do this
    288 	 * by checking to see if the intrhand points back to an intrhead
    289 	 * that points to our cia_config.  If not, it's an ISA IRQ.  Pretty
    290 	 * disgusting, eh?
    291 	 */
    292 	if (ih->ih_intrhead->intr_private != pc->pc_intr_v) {
    293 		sio_intr_disestablish(NULL /*XXX*/, cookie);
    294 		return;
    295 	}
    296 #endif
    297 
    298 	alpha_pci_generic_intr_disestablish(pc, cookie);
    299 }
    300 
    301 static void
    302 dec_550_intr_enable(pci_chipset_tag_t const pc __unused, int const irq)
    303 {
    304 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 1);
    305 }
    306 
    307 static void
    308 dec_550_intr_disable(pci_chipset_tag_t const pc __unused, int const irq)
    309 {
    310 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 0);
    311 }
    312