1 /* $OpenBSD: cia.c,v 1.25 2009/03/30 21:43:13 kettenis Exp $ */ 2 /* $NetBSD: cia.c,v 1.56 2000/06/29 08:58:45 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Author: Chris G. Demetriou 39 * 40 * Permission to use, copy, modify and distribute this software and 41 * its documentation is hereby granted, provided that both the copyright 42 * notice and this permission notice appear in all copies of the 43 * software, derivative works or modified versions, and any portions 44 * thereof, and that both notices appear in supporting documentation. 45 * 46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49 * 50 * Carnegie Mellon requests users of this software to return to 51 * 52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53 * School of Computer Science 54 * Carnegie Mellon University 55 * Pittsburgh PA 15213-3890 56 * 57 * any improvements or extensions that they make and grant Carnegie the 58 * rights to redistribute these changes. 59 */ 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/kernel.h> 64 #include <sys/malloc.h> 65 #include <sys/device.h> 66 #include <uvm/uvm_extern.h> 67 68 #include <machine/autoconf.h> 69 #include <machine/rpb.h> 70 71 #include <dev/isa/isareg.h> 72 #include <dev/isa/isavar.h> 73 74 #include <dev/pci/pcireg.h> 75 #include <dev/pci/pcivar.h> 76 #include <alpha/pci/ciareg.h> 77 #include <alpha/pci/ciavar.h> 78 79 #ifdef DEC_KN20AA 80 #include <alpha/pci/pci_kn20aa.h> 81 #endif 82 #ifdef DEC_EB164 83 #include <alpha/pci/pci_eb164.h> 84 #endif 85 #ifdef DEC_550 86 #include <alpha/pci/pci_550.h> 87 #endif 88 #ifdef DEC_1000A 89 #include <alpha/pci/pci_1000a.h> 90 #endif 91 #ifdef DEC_1000 92 #include <alpha/pci/pci_1000.h> 93 #endif 94 95 int ciamatch(struct device *, void *, void *); 96 void ciaattach(struct device *, struct device *, void *); 97 98 struct cfattach cia_ca = { 99 sizeof(struct device), ciamatch, ciaattach, 100 }; 101 102 struct cfdriver cia_cd = { 103 NULL, "cia", DV_DULL, 104 }; 105 106 static int ciaprint(void *, const char *pnp); 107 108 /* There can be only one. */ 109 int ciafound; 110 struct cia_config cia_configuration; 111 112 /* 113 * This determines if we attempt to use BWX for PCI bus and config space 114 * access. Some systems, notably with Pyxis, don't fare so well unless 115 * BWX is used. 116 * 117 * EXCEPT! Some devices have a really hard time if BWX is used (WHY?!). 118 * So, we decouple the uses for PCI config space and PCI bus space. 119 * 120 * FURTHERMORE! The Pyxis, most notably earlier revs, really don't 121 * do so well if you don't use BWX for bus access. So we default to 122 * forcing BWX on those chips. 123 * 124 * Geez. 125 */ 126 127 #ifndef CIA_PCI_USE_BWX 128 #define CIA_PCI_USE_BWX 1 129 #endif 130 131 #ifndef CIA_BUS_USE_BWX 132 #define CIA_BUS_USE_BWX 1 133 #endif 134 135 #ifndef CIA_PYXIS_FORCE_BWX 136 #define CIA_PYXIS_FORCE_BWX 1 137 #endif 138 139 int cia_pci_use_bwx = CIA_PCI_USE_BWX; 140 int cia_bus_use_bwx = CIA_BUS_USE_BWX; 141 int cia_pyxis_force_bwx = CIA_PYXIS_FORCE_BWX; 142 143 int 144 ciamatch(parent, match, aux) 145 struct device *parent; 146 void *match; 147 void *aux; 148 { 149 struct mainbus_attach_args *ma = aux; 150 151 /* Make sure that we're looking for a CIA. */ 152 if (strcmp(ma->ma_name, cia_cd.cd_name) != 0) 153 return (0); 154 155 if (ciafound) 156 return (0); 157 158 return (1); 159 } 160 161 /* 162 * Set up the chipset's function pointers. 163 */ 164 void 165 cia_init(ccp, mallocsafe) 166 struct cia_config *ccp; 167 int mallocsafe; 168 { 169 int pci_use_bwx = cia_pci_use_bwx; 170 int bus_use_bwx = cia_bus_use_bwx; 171 172 ccp->cc_hae_mem = REGVAL(CIA_CSR_HAE_MEM); 173 ccp->cc_hae_io = REGVAL(CIA_CSR_HAE_IO); 174 ccp->cc_rev = REGVAL(CIA_CSR_REV) & REV_MASK; 175 176 /* 177 * Determine if we have a Pyxis. Only two systypes can 178 * have this: the EB164 systype (AlphaPC164LX and AlphaPC164SX) 179 * and the DEC_550 systype (Miata). 180 */ 181 if ((cputype == ST_EB164 && 182 (hwrpb->rpb_variation & SV_ST_MASK) >= SV_ST_ALPHAPC164LX_400) || 183 cputype == ST_DEC_550) { 184 ccp->cc_flags |= CCF_ISPYXIS; 185 if (cia_pyxis_force_bwx) 186 pci_use_bwx = bus_use_bwx = 1; 187 } 188 189 /* 190 * ALCOR/ALCOR2 Revisions >= 2 and Pyxis have the CNFG register. 191 */ 192 if (ccp->cc_rev >= 2 || (ccp->cc_flags & CCF_ISPYXIS) != 0) 193 ccp->cc_cnfg = REGVAL(CIA_CSR_CNFG); 194 else 195 ccp->cc_cnfg = 0; 196 197 /* 198 * Use BWX iff: 199 * 200 * - It hasn't been disabled by the user, 201 * - it's enabled in CNFG, 202 * - we're implementation version ev5, 203 * - BWX is enabled in the CPU's capabilities mask (yes, 204 * the bit is really cleared if the capability exists...) 205 */ 206 if ((pci_use_bwx || bus_use_bwx) && 207 (ccp->cc_cnfg & CNFG_BWEN) != 0 && 208 (cpu_amask & ALPHA_AMASK_BWX) != 0) { 209 u_int32_t ctrl; 210 211 if (pci_use_bwx) 212 ccp->cc_flags |= CCF_PCI_USE_BWX; 213 if (bus_use_bwx) 214 ccp->cc_flags |= CCF_BUS_USE_BWX; 215 216 /* 217 * For whatever reason, the firmware seems to enable PCI 218 * loopback mode if it also enables BWX. Make sure it's 219 * enabled if we have an old, buggy firmware rev. 220 */ 221 alpha_mb(); 222 ctrl = REGVAL(CIA_CSR_CTRL); 223 if ((ctrl & CTRL_PCI_LOOP_EN) == 0) { 224 REGVAL(CIA_CSR_CTRL) = ctrl | CTRL_PCI_LOOP_EN; 225 alpha_mb(); 226 } 227 } 228 229 if (!ccp->cc_initted) { 230 /* don't do these twice since they set up extents */ 231 if (ccp->cc_flags & CCF_BUS_USE_BWX) { 232 cia_bwx_bus_io_init(&ccp->cc_iot, ccp); 233 cia_bwx_bus_mem_init(&ccp->cc_memt, ccp); 234 } else { 235 cia_bus_io_init(&ccp->cc_iot, ccp); 236 cia_bus_mem_init(&ccp->cc_memt, ccp); 237 } 238 } 239 ccp->cc_mallocsafe = mallocsafe; 240 241 cia_pci_init(&ccp->cc_pc, ccp); 242 alpha_pci_chipset = &ccp->cc_pc; 243 alpha_pci_chipset->pc_name = "cia"; 244 alpha_pci_chipset->pc_dense = CIA_PCI_DENSE; 245 alpha_pci_chipset->pc_hae_mask = 7L << 29; 246 if (ccp->cc_flags & CCF_BUS_USE_BWX) { 247 alpha_pci_chipset->pc_mem = CIA_EV56_BWMEM; 248 alpha_pci_chipset->pc_ports = CIA_EV56_BWIO; 249 alpha_pci_chipset->pc_bwx = 1; 250 } else { 251 alpha_pci_chipset->pc_mem = CIA_PCI_SMEM1; 252 alpha_pci_chipset->pc_ports = CIA_PCI_SIO1; 253 alpha_pci_chipset->pc_bwx = 0; 254 } 255 256 ccp->cc_initted = 1; 257 } 258 259 void 260 ciaattach(parent, self, aux) 261 struct device *parent, *self; 262 void *aux; 263 { 264 struct cia_config *ccp; 265 struct pcibus_attach_args pba; 266 const char *name; 267 int pass; 268 269 /* note that we've attached the chipset; can't have 2 CIAs. */ 270 ciafound = 1; 271 272 /* 273 * set up the chipset's info; done once at console init time 274 * (maybe), but we must do it here as well to take care of things 275 * that need to use memory allocation. 276 */ 277 ccp = &cia_configuration; 278 cia_init(ccp, 1); 279 280 if (ccp->cc_flags & CCF_ISPYXIS) { 281 name = "Pyxis"; 282 pass = ccp->cc_rev; 283 } else { 284 name = "ALCOR/ALCOR2"; 285 pass = ccp->cc_rev + 1; 286 } 287 288 printf(": DECchip 2117x Core Logic Chipset (%s), pass %d\n", 289 name, pass); 290 291 if (ccp->cc_cnfg) 292 printf("%s: extended capabilities: %b\n", self->dv_xname, 293 ccp->cc_cnfg, CIA_CSR_CNFG_BITS); 294 295 switch (ccp->cc_flags & (CCF_PCI_USE_BWX|CCF_BUS_USE_BWX)) { 296 case CCF_PCI_USE_BWX|CCF_BUS_USE_BWX: 297 name = "PCI config and bus"; 298 break; 299 case CCF_PCI_USE_BWX: 300 name = "PCI config"; 301 break; 302 case CCF_BUS_USE_BWX: 303 name = "bus"; 304 break; 305 default: 306 name = NULL; 307 break; 308 } 309 if (name != NULL) 310 printf("%s: using BWX for %s access\n", self->dv_xname, name); 311 312 #ifdef DEC_550 313 if (cputype == ST_DEC_550 && 314 (hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) { 315 /* 316 * Miata 1 systems have a bug: DMA cannot cross 317 * an 8k boundary! Make sure PCI read prefetching 318 * is disabled on these chips. Note that secondary 319 * PCI busses don't have this problem, because of 320 * the way PPBs handle PCI read requests. 321 * 322 * In the 21174 Technical Reference Manual, this is 323 * actually documented as "Pyxis Pass 1", but apparently 324 * there are chips that report themselves as "Pass 1" 325 * which do not have the bug! Miatas with the Cypress 326 * PCI-ISA bridge (i.e. Miata 1.5 and Miata 2) do not 327 * have the bug, so we use this check. 328 * 329 * NOTE: This bug is actually worked around in cia_dma.c, 330 * when direct-mapped DMA maps are created. 331 * 332 * XXX WE NEED TO THINK ABOUT HOW TO HANDLE THIS FOR 333 * XXX SGMAP DMA MAPPINGS! 334 */ 335 u_int32_t ctrl; 336 337 /* XXX no bets... */ 338 printf("%s: WARNING: Pyxis pass 1 DMA bug; no bets...\n", 339 self->dv_xname); 340 341 ccp->cc_flags |= CCF_PYXISBUG; 342 343 alpha_mb(); 344 ctrl = REGVAL(CIA_CSR_CTRL); 345 ctrl &= ~(CTRL_RD_TYPE|CTRL_RL_TYPE|CTRL_RM_TYPE); 346 REGVAL(CIA_CSR_CTRL) = ctrl; 347 alpha_mb(); 348 } 349 #endif /* DEC_550 */ 350 351 cia_dma_init(ccp); 352 353 switch (cputype) { 354 #ifdef DEC_KN20AA 355 case ST_DEC_KN20AA: 356 pci_kn20aa_pickintr(ccp); 357 break; 358 #endif 359 360 #ifdef DEC_EB164 361 case ST_EB164: 362 pci_eb164_pickintr(ccp); 363 break; 364 #endif 365 366 #ifdef DEC_550 367 case ST_DEC_550: 368 pci_550_pickintr(ccp); 369 break; 370 #endif 371 372 #ifdef DEC_1000A 373 case ST_DEC_1000A: 374 pci_1000a_pickintr(ccp, &ccp->cc_iot, &ccp->cc_memt, 375 &ccp->cc_pc); 376 break; 377 #endif 378 379 #ifdef DEC_1000 380 case ST_DEC_1000: 381 pci_1000_pickintr(ccp, &ccp->cc_iot, &ccp->cc_memt, 382 &ccp->cc_pc); 383 break; 384 #endif 385 386 default: 387 panic("ciaattach: shouldn't be here, really..."); 388 } 389 390 bzero(&pba, sizeof(pba)); 391 pba.pba_busname = "pci"; 392 pba.pba_iot = &ccp->cc_iot; 393 pba.pba_memt = &ccp->cc_memt; 394 pba.pba_dmat = 395 alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI); 396 pba.pba_pc = &ccp->cc_pc; 397 pba.pba_domain = pci_ndomains++; 398 pba.pba_bus = 0; 399 #ifdef notyet 400 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED; 401 if ((ccp->cc_flags & CCF_PYXISBUG) == 0) 402 pba.pba_flags |= PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | 403 PCI_FLAGS_MWI_OKAY; 404 #endif 405 config_found(self, &pba, ciaprint); 406 } 407 408 static int 409 ciaprint(aux, pnp) 410 void *aux; 411 const char *pnp; 412 { 413 register struct pcibus_attach_args *pba = aux; 414 415 /* only PCIs can attach to CIAs; easy. */ 416 if (pnp) 417 printf("%s at %s", pba->pba_busname, pnp); 418 printf(" bus %d", pba->pba_bus); 419 return (UNCONF); 420 } 421 422 void 423 cia_pyxis_intr_enable(irq, onoff) 424 int irq, onoff; 425 { 426 u_int64_t imask; 427 int s; 428 429 #if 0 430 printf("cia_pyxis_intr_enable: %s %d\n", 431 onoff ? "enabling" : "disabling", irq); 432 #endif 433 434 s = splhigh(); 435 alpha_mb(); 436 imask = REGVAL64(PYXIS_INT_MASK); 437 if (onoff) 438 imask |= (1UL << irq); 439 else 440 imask &= ~(1UL << irq); 441 REGVAL64(PYXIS_INT_MASK) = imask; 442 alpha_mb(); 443 splx(s); 444 } 445