Home | History | Annotate | Line # | Download | only in pci
pci_550.c revision 1.39
      1 /* $NetBSD: pci_550.c,v 1.39 2021/06/19 16:59:07 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.39 2021/06/19 16:59:07 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 	char *cp;
    125 	int i;
    126 
    127 	pc->pc_intr_v = core;
    128 	pc->pc_intr_map = dec_550_intr_map;
    129 	pc->pc_intr_string = dec_550_intr_string;
    130 	pc->pc_intr_evcnt = dec_550_intr_evcnt;
    131 	pc->pc_intr_establish = dec_550_intr_establish;
    132 	pc->pc_intr_disestablish = dec_550_intr_disestablish;
    133 
    134 	pc->pc_pciide_compat_intr_establish =
    135 	    sio_pciide_compat_intr_establish;
    136 
    137 	/*
    138 	 * DEC 550's interrupts are enabled via the Pyxis interrupt
    139 	 * mask register.  Nothing to map.
    140 	 */
    141 
    142 	for (i = 0; i < DEC_550_MAX_IRQ; i++)
    143 		dec_550_intr_disable(pc, i);
    144 
    145 #define PCI_550_IRQ_STR	8
    146 	pc->pc_shared_intrs = alpha_shared_intr_alloc(DEC_550_MAX_IRQ,
    147 	    PCI_550_IRQ_STR);
    148 	pc->pc_intr_desc = "dec 550 irq";
    149 	pc->pc_vecbase = 0x900;
    150 	pc->pc_nirq = DEC_550_MAX_IRQ;
    151 
    152 	pc->pc_intr_enable = dec_550_intr_enable;
    153 	pc->pc_intr_disable = dec_550_intr_disable;
    154 
    155 	for (i = 0; i < DEC_550_MAX_IRQ; i++) {
    156 		alpha_shared_intr_set_maxstrays(pc->pc_shared_intrs, i,
    157 		    PCI_STRAY_MAX);
    158 		alpha_shared_intr_set_private(pc->pc_shared_intrs, i, core);
    159 
    160 		cp = alpha_shared_intr_string(pc->pc_shared_intrs, i);
    161 		snprintf(cp, PCI_550_IRQ_STR, "irq %d", i);
    162 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    163 		    pc->pc_shared_intrs, i), EVCNT_TYPE_INTR, NULL,
    164 		    "dec 550", cp);
    165 	}
    166 
    167 #if NSIO
    168 	sio_intr_setup(pc, iot);
    169 #endif
    170 }
    171 ALPHA_PCI_INTR_INIT(ST_DEC_550, pci_550_pickintr)
    172 
    173 static int
    174 dec_550_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    175 {
    176 	pcitag_t bustag = pa->pa_intrtag;
    177 	int buspin = pa->pa_intrpin, line = pa->pa_intrline;
    178 	pci_chipset_tag_t pc = pa->pa_pc;
    179 	int bus, device, function;
    180 
    181 	if (buspin == 0) {
    182 		/* No IRQ used. */
    183 		return 1;
    184 	}
    185 	if (buspin < 0 || buspin > 4) {
    186 		printf("dec_550_intr_map: bad interrupt pin %d\n", buspin);
    187 		return 1;
    188 	}
    189 
    190 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
    191 
    192 	/*
    193 	 * There are two main variants of Miata: Miata 1 (Intel SIO)
    194 	 * and Miata {1.5,2} (Cypress).
    195 	 *
    196 	 * The Miata 1 has a CMD PCI IDE wired to compatibility mode at
    197 	 * device 4 of bus 0.  This variant apparently also has the
    198 	 * Pyxis DMA bug.
    199 	 *
    200 	 * On the Miata 1.5 and Miata 2, the Cypress PCI-ISA bridge lives
    201 	 * on device 7 of bus 0.  This device has PCI IDE wired to
    202 	 * compatibility mode on functions 1 and 2.
    203 	 *
    204 	 * There will be no interrupt mapping for these devices, so just
    205 	 * bail out now.
    206 	 */
    207 	if (bus == 0) {
    208 		if ((hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
    209 			/* Miata 1 */
    210 			if (device == 7)
    211 				panic("dec_550_intr_map: SIO device");
    212 			else if (device == 4)
    213 				return (1);
    214 		} else {
    215 			/* Miata 1.5 or Miata 2 */
    216 			if (device == 7) {
    217 				if (function == 0)
    218 					panic("dec_550_intr_map: SIO device");
    219 				if (function == 1 || function == 2)
    220 					return (1);
    221 			}
    222 		}
    223 	}
    224 
    225 	/*
    226 	 * The console places the interrupt mapping in the "line" value.
    227 	 * A value of (char)-1 indicates there is no mapping.
    228 	 */
    229 	if (line == 0xff) {
    230 		printf("dec_550_intr_map: no mapping for %d/%d/%d\n",
    231 		    bus, device, function);
    232 		return (1);
    233 	}
    234 
    235 #if NSIO == 0
    236 	if (DEC_550_LINE_IS_ISA(line)) {
    237 		printf("dec_550_intr_map: ISA IRQ %d for %d/%d/%d\n",
    238 		    DEC_550_LINE_ISA_IRQ(line), bus, device, function);
    239 		return (1);
    240 	}
    241 #endif
    242 
    243 	if (DEC_550_LINE_IS_ISA(line) == 0 && line >= DEC_550_MAX_IRQ) {
    244 		printf("dec_550_intr_map: irq %d out of range %d/%d/%d\n",
    245 		    line, bus, device, function);
    246 		return (1);
    247 	}
    248 	alpha_pci_intr_handle_init(ihp, line, 0);
    249 	return (0);
    250 }
    251 
    252 static const char *
    253 dec_550_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    254     char * const buf, size_t const len)
    255 {
    256 #if NSIO
    257 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    258 
    259 	if (DEC_550_LINE_IS_ISA(irq))
    260 		return sio_intr_string(NULL /*XXX*/,
    261 		    DEC_550_LINE_ISA_IRQ(irq), buf, len);
    262 #endif
    263 
    264 	return alpha_pci_generic_intr_string(pc, ih, buf, len);
    265 }
    266 
    267 static const struct evcnt *
    268 dec_550_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
    269 {
    270 #if NSIO
    271 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    272 
    273 	if (DEC_550_LINE_IS_ISA(irq))
    274 		return (sio_intr_evcnt(NULL /*XXX*/,
    275 		    DEC_550_LINE_ISA_IRQ(irq)));
    276 #endif
    277 
    278 	return alpha_pci_generic_intr_evcnt(pc, ih);
    279 }
    280 
    281 static void *
    282 dec_550_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    283     int const level, int (*func)(void *), void *arg)
    284 {
    285 #if NSIO
    286 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    287 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    288 
    289 	if (DEC_550_LINE_IS_ISA(irq))
    290 		return (sio_intr_establish(NULL /*XXX*/,
    291 		    DEC_550_LINE_ISA_IRQ(irq), IST_LEVEL, level, flags,
    292 		    func, arg));
    293 #endif
    294 
    295 	return alpha_pci_generic_intr_establish(pc, ih, level, func, arg);
    296 }
    297 
    298 static void
    299 dec_550_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
    300 {
    301 #if NSIO
    302 	struct alpha_shared_intrhand * const ih = cookie;
    303 
    304 	/*
    305 	 * We have to determine if this is an ISA IRQ or not!  We do this
    306 	 * by checking to see if the intrhand points back to an intrhead
    307 	 * that points to our cia_config.  If not, it's an ISA IRQ.  Pretty
    308 	 * disgusting, eh?
    309 	 */
    310 	if (ih->ih_intrhead->intr_private != pc->pc_intr_v) {
    311 		sio_intr_disestablish(NULL /*XXX*/, cookie);
    312 		return;
    313 	}
    314 #endif
    315 
    316 	alpha_pci_generic_intr_disestablish(pc, cookie);
    317 }
    318 
    319 static void
    320 dec_550_intr_enable(pci_chipset_tag_t const pc __unused, int const irq)
    321 {
    322 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 1);
    323 }
    324 
    325 static void
    326 dec_550_intr_disable(pci_chipset_tag_t const pc __unused, int const irq)
    327 {
    328 	cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 0);
    329 }
    330