1 /* $NetBSD: pci_eb164.c,v 1.41 2010/12/15 01:27:19 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 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. 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@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_eb164.c,v 1.41 2010/12/15 01:27:19 matt 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_eb164.h> 85 86 #include "sio.h" 87 #if NSIO 88 #include <alpha/pci/siovar.h> 89 #endif 90 91 int dec_eb164_intr_map(struct pci_attach_args *, pci_intr_handle_t *); 92 const char *dec_eb164_intr_string(void *, pci_intr_handle_t); 93 const struct evcnt *dec_eb164_intr_evcnt(void *, pci_intr_handle_t); 94 void *dec_eb164_intr_establish(void *, pci_intr_handle_t, 95 int, int (*func)(void *), void *); 96 void dec_eb164_intr_disestablish(void *, void *); 97 98 void *dec_eb164_pciide_compat_intr_establish(void *, struct device *, 99 struct pci_attach_args *, int, int (*)(void *), void *); 100 101 #define EB164_SIO_IRQ 4 102 #define EB164_MAX_IRQ 24 103 #define PCI_STRAY_MAX 5 104 105 struct alpha_shared_intr *eb164_pci_intr; 106 107 bus_space_tag_t eb164_intrgate_iot; 108 bus_space_handle_t eb164_intrgate_ioh; 109 110 void eb164_iointr(void *arg, unsigned long vec); 111 extern void eb164_intr_enable(int irq); /* pci_eb164_intr.S */ 112 extern void eb164_intr_disable(int irq); /* pci_eb164_intr.S */ 113 114 void 115 pci_eb164_pickintr(struct cia_config *ccp) 116 { 117 bus_space_tag_t iot = &ccp->cc_iot; 118 pci_chipset_tag_t pc = &ccp->cc_pc; 119 char *cp; 120 int i; 121 122 pc->pc_intr_v = ccp; 123 pc->pc_intr_map = dec_eb164_intr_map; 124 pc->pc_intr_string = dec_eb164_intr_string; 125 pc->pc_intr_evcnt = dec_eb164_intr_evcnt; 126 pc->pc_intr_establish = dec_eb164_intr_establish; 127 pc->pc_intr_disestablish = dec_eb164_intr_disestablish; 128 129 pc->pc_pciide_compat_intr_establish = 130 dec_eb164_pciide_compat_intr_establish; 131 132 eb164_intrgate_iot = iot; 133 if (bus_space_map(eb164_intrgate_iot, 0x804, 3, 0, 134 &eb164_intrgate_ioh) != 0) 135 panic("pci_eb164_pickintr: couldn't map interrupt PLD"); 136 for (i = 0; i < EB164_MAX_IRQ; i++) 137 eb164_intr_disable(i); 138 139 eb164_pci_intr = alpha_shared_intr_alloc(EB164_MAX_IRQ, 8); 140 for (i = 0; i < EB164_MAX_IRQ; i++) { 141 /* 142 * Systems with a Pyxis seem to have problems with 143 * stray interrupts, so just ignore them. Sigh, 144 * I hate buggy hardware. 145 */ 146 alpha_shared_intr_set_maxstrays(eb164_pci_intr, i, 147 (ccp->cc_flags & CCF_ISPYXIS) ? 0 : PCI_STRAY_MAX); 148 149 cp = alpha_shared_intr_string(eb164_pci_intr, i); 150 sprintf(cp, "irq %d", i); 151 evcnt_attach_dynamic(alpha_shared_intr_evcnt( 152 eb164_pci_intr, i), EVCNT_TYPE_INTR, NULL, 153 "eb164", cp); 154 } 155 156 #if NSIO 157 sio_intr_setup(pc, iot); 158 eb164_intr_enable(EB164_SIO_IRQ); 159 #endif 160 } 161 162 int 163 dec_eb164_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 164 { 165 pcitag_t bustag = pa->pa_intrtag; 166 int buspin = pa->pa_intrpin, line = pa->pa_intrline; 167 pci_chipset_tag_t pc = pa->pa_pc; 168 int bus, device, function; 169 u_int64_t variation; 170 171 if (buspin == 0) { 172 /* No IRQ used. */ 173 return 1; 174 } 175 if (buspin > 4) { 176 printf("dec_eb164_intr_map: bad interrupt pin %d\n", buspin); 177 return 1; 178 } 179 180 pci_decompose_tag(pc, bustag, &bus, &device, &function); 181 182 variation = hwrpb->rpb_variation & SV_ST_MASK; 183 184 /* 185 * 186 * The AlphaPC 164 and AlphaPC 164LX have a CMD PCI IDE controller 187 * at bus 0 device 11. These are wired to compatibility mode, 188 * so do not map their interrupts. 189 * 190 * The AlphaPC 164SX has PCI IDE on functions 1 and 2 of the 191 * Cypress PCI-ISA bridge at bus 0 device 8. These, too, are 192 * wired to compatibility mode. 193 * 194 * Real EB164s have ISA IDE on the Super I/O chip. 195 */ 196 if (bus == 0) { 197 if (variation >= SV_ST_ALPHAPC164_366 && 198 variation <= SV_ST_ALPHAPC164LX_600) { 199 if (device == 8) 200 panic("dec_eb164_intr_map: SIO device"); 201 if (device == 11) 202 return (1); 203 } else if (variation >= SV_ST_ALPHAPC164SX_400 && 204 variation <= SV_ST_ALPHAPC164SX_600) { 205 if (device == 8) { 206 if (function == 0) 207 panic("dec_eb164_intr_map: SIO device"); 208 return (1); 209 } 210 } else { 211 if (device == 8) 212 panic("dec_eb164_intr_map: SIO device"); 213 } 214 } 215 216 /* 217 * The console places the interrupt mapping in the "line" value. 218 * A value of (char)-1 indicates there is no mapping. 219 */ 220 if (line == 0xff) { 221 printf("dec_eb164_intr_map: no mapping for %d/%d/%d\n", 222 bus, device, function); 223 return (1); 224 } 225 226 if (line > EB164_MAX_IRQ) 227 panic("dec_eb164_intr_map: eb164 irq too large (%d)", 228 line); 229 230 *ihp = line; 231 return (0); 232 } 233 234 const char * 235 dec_eb164_intr_string(void *ccv, pci_intr_handle_t ih) 236 { 237 #if 0 238 struct cia_config *ccp = ccv; 239 #endif 240 static char irqstr[15]; /* 11 + 2 + NULL + sanity */ 241 242 if (ih > EB164_MAX_IRQ) 243 panic("dec_eb164_intr_string: bogus eb164 IRQ 0x%lx", ih); 244 sprintf(irqstr, "eb164 irq %ld", ih); 245 return (irqstr); 246 } 247 248 const struct evcnt * 249 dec_eb164_intr_evcnt(void *ccv, pci_intr_handle_t ih) 250 { 251 #if 0 252 struct cia_config *ccp = ccv; 253 #endif 254 255 if (ih > EB164_MAX_IRQ) 256 panic("dec_eb164_intr_string: bogus eb164 IRQ 0x%lx", ih); 257 return (alpha_shared_intr_evcnt(eb164_pci_intr, ih)); 258 } 259 260 void * 261 dec_eb164_intr_establish(void *ccv, pci_intr_handle_t ih, int level, int (*func)(void *), void *arg) 262 { 263 #if 0 264 struct cia_config *ccp = ccv; 265 #endif 266 void *cookie; 267 268 if (ih > EB164_MAX_IRQ) 269 panic("dec_eb164_intr_establish: bogus eb164 IRQ 0x%lx", ih); 270 271 cookie = alpha_shared_intr_establish(eb164_pci_intr, ih, IST_LEVEL, 272 level, func, arg, "eb164 irq"); 273 274 if (cookie != NULL && 275 alpha_shared_intr_firstactive(eb164_pci_intr, ih)) { 276 scb_set(0x900 + SCB_IDXTOVEC(ih), eb164_iointr, NULL, 277 level); 278 eb164_intr_enable(ih); 279 } 280 return (cookie); 281 } 282 283 void 284 dec_eb164_intr_disestablish(void *ccv, void *cookie) 285 { 286 #if 0 287 struct cia_config *ccp = ccv; 288 #endif 289 struct alpha_shared_intrhand *ih = cookie; 290 unsigned int irq = ih->ih_num; 291 int s; 292 293 s = splhigh(); 294 295 alpha_shared_intr_disestablish(eb164_pci_intr, cookie, 296 "eb164 irq"); 297 if (alpha_shared_intr_isactive(eb164_pci_intr, irq) == 0) { 298 eb164_intr_disable(irq); 299 alpha_shared_intr_set_dfltsharetype(eb164_pci_intr, irq, 300 IST_NONE); 301 scb_free(0x900 + SCB_IDXTOVEC(irq)); 302 } 303 304 splx(s); 305 } 306 307 void * 308 dec_eb164_pciide_compat_intr_establish(void *v, struct device *dev, struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg) 309 { 310 pci_chipset_tag_t pc = pa->pa_pc; 311 void *cookie = NULL; 312 int bus, irq; 313 314 pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL); 315 316 /* 317 * If this isn't PCI bus #0, all bets are off. 318 */ 319 if (bus != 0) 320 return (NULL); 321 322 irq = PCIIDE_COMPAT_IRQ(chan); 323 #if NSIO 324 cookie = sio_intr_establish(NULL /*XXX*/, irq, IST_EDGE, IPL_BIO, 325 func, arg); 326 if (cookie == NULL) 327 return (NULL); 328 printf("%s: %s channel interrupting at %s\n", dev->dv_xname, 329 PCIIDE_CHANNEL_NAME(chan), sio_intr_string(NULL /*XXX*/, irq)); 330 #endif 331 return (cookie); 332 } 333 334 void 335 eb164_iointr(void *arg, unsigned long vec) 336 { 337 int irq; 338 339 irq = SCB_VECTOIDX(vec - 0x900); 340 341 if (!alpha_shared_intr_dispatch(eb164_pci_intr, irq)) { 342 alpha_shared_intr_stray(eb164_pci_intr, irq, 343 "eb164 irq"); 344 if (ALPHA_SHARED_INTR_DISABLE(eb164_pci_intr, irq)) 345 eb164_intr_disable(irq); 346 } else 347 alpha_shared_intr_reset_strays(eb164_pci_intr, irq); 348 } 349 350 #if 0 /* THIS DOES NOT WORK! see pci_eb164_intr.S. */ 351 u_int8_t eb164_intr_mask[3] = { 0xff, 0xff, 0xff }; 352 353 void 354 eb164_intr_enable(int irq) 355 { 356 int byte = (irq / 8), bit = (irq % 8); 357 358 #if 1 359 printf("eb164_intr_enable: enabling %d (%d:%d)\n", irq, byte, bit); 360 #endif 361 eb164_intr_mask[byte] &= ~(1 << bit); 362 363 bus_space_write_1(eb164_intrgate_iot, eb164_intrgate_ioh, byte, 364 eb164_intr_mask[byte]); 365 } 366 367 void 368 eb164_intr_disable(int irq) 369 { 370 int byte = (irq / 8), bit = (irq % 8); 371 372 #if 1 373 printf("eb164_intr_disable: disabling %d (%d:%d)\n", irq, byte, bit); 374 #endif 375 eb164_intr_mask[byte] |= (1 << bit); 376 377 bus_space_write_1(eb164_intrgate_iot, eb164_intrgate_ioh, byte, 378 eb164_intr_mask[byte]); 379 } 380 #endif 381