1 /* $NetBSD: pci_kn20aa.c,v 1.54 2014/03/21 16:39:29 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 31 32 __KERNEL_RCSID(0, "$NetBSD: pci_kn20aa.c,v 1.54 2014/03/21 16:39:29 christos Exp $"); 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/systm.h> 38 #include <sys/errno.h> 39 #include <sys/malloc.h> 40 #include <sys/device.h> 41 #include <sys/syslog.h> 42 43 #include <machine/autoconf.h> 44 45 #include <dev/pci/pcireg.h> 46 #include <dev/pci/pcivar.h> 47 48 #include <alpha/pci/ciareg.h> 49 #include <alpha/pci/ciavar.h> 50 51 #include <alpha/pci/pci_kn20aa.h> 52 53 #include "sio.h" 54 #if NSIO > 0 || NPCEB > 0 55 #include <alpha/pci/siovar.h> 56 #endif 57 58 int dec_kn20aa_intr_map(const struct pci_attach_args *, 59 pci_intr_handle_t *); 60 const char *dec_kn20aa_intr_string(void *, pci_intr_handle_t, char *, size_t); 61 const struct evcnt *dec_kn20aa_intr_evcnt(void *, pci_intr_handle_t); 62 void *dec_kn20aa_intr_establish(void *, pci_intr_handle_t, 63 int, int (*func)(void *), void *); 64 void dec_kn20aa_intr_disestablish(void *, void *); 65 66 #define KN20AA_PCEB_IRQ 31 67 #define KN20AA_MAX_IRQ 32 68 #define PCI_STRAY_MAX 5 69 70 struct alpha_shared_intr *kn20aa_pci_intr; 71 72 void kn20aa_iointr(void *arg, unsigned long vec); 73 void kn20aa_enable_intr(int irq); 74 void kn20aa_disable_intr(int irq); 75 76 void 77 pci_kn20aa_pickintr(struct cia_config *ccp) 78 { 79 int i; 80 #if NSIO > 0 || NPCEB > 0 81 bus_space_tag_t iot = &ccp->cc_iot; 82 #endif 83 pci_chipset_tag_t pc = &ccp->cc_pc; 84 char *cp; 85 86 pc->pc_intr_v = ccp; 87 pc->pc_intr_map = dec_kn20aa_intr_map; 88 pc->pc_intr_string = dec_kn20aa_intr_string; 89 pc->pc_intr_evcnt = dec_kn20aa_intr_evcnt; 90 pc->pc_intr_establish = dec_kn20aa_intr_establish; 91 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish; 92 93 /* Not supported on KN20AA. */ 94 pc->pc_pciide_compat_intr_establish = NULL; 95 96 #define PCI_KN20AA_IRQ_STR 8 97 kn20aa_pci_intr = alpha_shared_intr_alloc(KN20AA_MAX_IRQ, 98 PCI_KN20AA_IRQ_STR); 99 for (i = 0; i < KN20AA_MAX_IRQ; i++) { 100 alpha_shared_intr_set_maxstrays(kn20aa_pci_intr, i, 101 PCI_STRAY_MAX); 102 103 cp = alpha_shared_intr_string(kn20aa_pci_intr, i); 104 snprintf(cp, PCI_KN20AA_IRQ_STR, "irq %d", i); 105 evcnt_attach_dynamic(alpha_shared_intr_evcnt( 106 kn20aa_pci_intr, i), EVCNT_TYPE_INTR, NULL, 107 "kn20aa", cp); 108 } 109 110 #if NSIO > 0 || NPCEB > 0 111 sio_intr_setup(pc, iot); 112 kn20aa_enable_intr(KN20AA_PCEB_IRQ); 113 #endif 114 } 115 116 int 117 dec_kn20aa_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 118 { 119 pcitag_t bustag = pa->pa_intrtag; 120 int buspin = pa->pa_intrpin; 121 pci_chipset_tag_t pc = pa->pa_pc; 122 int device; 123 int kn20aa_irq; 124 125 if (buspin == 0) { 126 /* No IRQ used. */ 127 return 1; 128 } 129 if (buspin > 4) { 130 printf("dec_kn20aa_intr_map: bad interrupt pin %d\n", buspin); 131 return 1; 132 } 133 134 /* 135 * Slot->interrupt translation. Appears to work, though it 136 * may not hold up forever. 137 * 138 * The DEC engineers who did this hardware obviously engaged 139 * in random drug testing. 140 */ 141 pci_decompose_tag(pc, bustag, NULL, &device, NULL); 142 switch (device) { 143 case 11: 144 case 12: 145 kn20aa_irq = ((device - 11) + 0) * 4; 146 break; 147 148 case 7: 149 kn20aa_irq = 8; 150 break; 151 152 case 9: 153 kn20aa_irq = 12; 154 break; 155 156 case 6: /* 21040 on AlphaStation 500 */ 157 kn20aa_irq = 13; 158 break; 159 160 case 8: 161 kn20aa_irq = 16; 162 break; 163 164 default: 165 printf("dec_kn20aa_intr_map: weird device number %d\n", 166 device); 167 return 1; 168 } 169 170 kn20aa_irq += buspin - 1; 171 if (kn20aa_irq > KN20AA_MAX_IRQ) 172 panic("dec_kn20aa_intr_map: kn20aa_irq too large (%d)", 173 kn20aa_irq); 174 175 *ihp = kn20aa_irq; 176 return (0); 177 } 178 179 const char * 180 dec_kn20aa_intr_string(void *ccv, pci_intr_handle_t ih, char *buf, size_t len) 181 { 182 #if 0 183 struct cia_config *ccp = ccv; 184 #endif 185 if (ih > KN20AA_MAX_IRQ) 186 panic("%s: bogus kn20aa IRQ 0x%lx", __func__, ih); 187 188 snprintf(buf, len, "kn20aa irq %ld", ih); 189 return buf; 190 } 191 192 const struct evcnt * 193 dec_kn20aa_intr_evcnt(void *ccv, pci_intr_handle_t ih) 194 { 195 #if 0 196 struct cia_config *ccp = ccv; 197 #endif 198 199 if (ih > KN20AA_MAX_IRQ) 200 panic("%s: bogus kn20aa IRQ 0x%lx", __func__, ih); 201 return (alpha_shared_intr_evcnt(kn20aa_pci_intr, ih)); 202 } 203 204 void * 205 dec_kn20aa_intr_establish( 206 void *ccv, 207 pci_intr_handle_t ih, 208 int level, 209 int (*func)(void *), 210 void *arg) 211 { 212 #if 0 213 struct cia_config *ccp = ccv; 214 #endif 215 void *cookie; 216 217 if (ih > KN20AA_MAX_IRQ) 218 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%lx", 219 ih); 220 221 cookie = alpha_shared_intr_establish(kn20aa_pci_intr, ih, IST_LEVEL, 222 level, func, arg, "kn20aa irq"); 223 224 if (cookie != NULL && 225 alpha_shared_intr_firstactive(kn20aa_pci_intr, ih)) { 226 scb_set(0x900 + SCB_IDXTOVEC(ih), kn20aa_iointr, NULL, 227 level); 228 kn20aa_enable_intr(ih); 229 } 230 return (cookie); 231 } 232 233 void 234 dec_kn20aa_intr_disestablish(void *ccv, void *cookie) 235 { 236 #if 0 237 struct cia_config *ccp = ccv; 238 #endif 239 struct alpha_shared_intrhand *ih = cookie; 240 unsigned int irq = ih->ih_num; 241 int s; 242 243 s = splhigh(); 244 245 alpha_shared_intr_disestablish(kn20aa_pci_intr, cookie, 246 "kn20aa irq"); 247 if (alpha_shared_intr_isactive(kn20aa_pci_intr, irq) == 0) { 248 kn20aa_disable_intr(irq); 249 alpha_shared_intr_set_dfltsharetype(kn20aa_pci_intr, irq, 250 IST_NONE); 251 scb_free(0x900 + SCB_IDXTOVEC(irq)); 252 } 253 254 splx(s); 255 } 256 257 void 258 kn20aa_iointr(void *arg, unsigned long vec) 259 { 260 int irq; 261 262 irq = SCB_VECTOIDX(vec - 0x900); 263 264 if (!alpha_shared_intr_dispatch(kn20aa_pci_intr, irq)) { 265 alpha_shared_intr_stray(kn20aa_pci_intr, irq, 266 "kn20aa irq"); 267 if (ALPHA_SHARED_INTR_DISABLE(kn20aa_pci_intr, irq)) 268 kn20aa_disable_intr(irq); 269 } else 270 alpha_shared_intr_reset_strays(kn20aa_pci_intr, irq); 271 } 272 273 void 274 kn20aa_enable_intr(int irq) 275 { 276 277 /* 278 * From disassembling small bits of the OSF/1 kernel: 279 * the following appears to enable a given interrupt request. 280 * "blech." I'd give valuable body parts for better docs or 281 * for a good decompiler. 282 */ 283 alpha_mb(); 284 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */ 285 alpha_mb(); 286 } 287 288 void 289 kn20aa_disable_intr(int irq) 290 { 291 292 alpha_mb(); 293 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */ 294 alpha_mb(); 295 } 296