Home | History | Annotate | Line # | Download | only in pci
cia.c revision 1.76
      1 /* $NetBSD: cia.c,v 1.76 2021/06/18 22:17:53 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.
     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 "opt_dec_eb164.h"
     61 #include "opt_dec_kn20aa.h"
     62 #include "opt_dec_550.h"
     63 #include "opt_dec_1000a.h"
     64 #include "opt_dec_1000.h"
     65 
     66 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     67 
     68 __KERNEL_RCSID(0, "$NetBSD: cia.c,v 1.76 2021/06/18 22:17:53 thorpej Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/kernel.h>
     73 #include <sys/malloc.h>
     74 #include <sys/device.h>
     75 
     76 #include <machine/autoconf.h>
     77 #include <machine/rpb.h>
     78 #include <machine/sysarch.h>
     79 #include <machine/alpha.h>
     80 
     81 #include <dev/isa/isareg.h>
     82 #include <dev/isa/isavar.h>
     83 
     84 #include <dev/pci/pcireg.h>
     85 #include <dev/pci/pcivar.h>
     86 #include <alpha/pci/ciareg.h>
     87 #include <alpha/pci/ciavar.h>
     88 
     89 #ifdef DEC_KN20AA
     90 #include <alpha/pci/pci_kn20aa.h>
     91 #endif
     92 #ifdef DEC_EB164
     93 #include <alpha/pci/pci_eb164.h>
     94 #endif
     95 #ifdef DEC_550
     96 #include <alpha/pci/pci_550.h>
     97 #endif
     98 #ifdef DEC_1000A
     99 #include <alpha/pci/pci_1000a.h>
    100 #endif
    101 #ifdef DEC_1000
    102 #include <alpha/pci/pci_1000.h>
    103 #endif
    104 
    105 static int	ciamatch(device_t, cfdata_t, void *);
    106 static void	ciaattach(device_t, device_t, void *);
    107 
    108 CFATTACH_DECL_NEW(cia, sizeof(struct cia_softc),
    109     ciamatch, ciaattach, NULL, NULL);
    110 
    111 extern struct cfdriver cia_cd;
    112 
    113 static int	cia_bus_get_window(int, int,
    114 		    struct alpha_bus_space_translation *);
    115 
    116 /* There can be only one. */
    117 static int ciafound;
    118 struct cia_config cia_configuration;
    119 
    120 /*
    121  * This determines if we attempt to use BWX for PCI bus and config space
    122  * access.  Some systems, notably with Pyxis, don't fare so well unless
    123  * BWX is used.
    124  *
    125  * EXCEPT!  Some devices have a really hard time if BWX is used (WHY?!).
    126  * So, we decouple the uses for PCI config space and PCI bus space.
    127  *
    128  * FURTHERMORE!  The Pyxis, most notably earlier revs, really don't
    129  * do so well if you don't use BWX for bus access.  So we default to
    130  * forcing BWX on those chips.
    131  *
    132  * Geez.
    133  */
    134 
    135 #ifndef CIA_PCI_USE_BWX
    136 #define	CIA_PCI_USE_BWX	1
    137 #endif
    138 
    139 #ifndef	CIA_BUS_USE_BWX
    140 #define	CIA_BUS_USE_BWX	0
    141 #endif
    142 
    143 #ifndef CIA_PYXIS_FORCE_BWX
    144 #define	CIA_PYXIS_FORCE_BWX 0
    145 #endif
    146 
    147 int	cia_pci_use_bwx = CIA_PCI_USE_BWX;
    148 int	cia_bus_use_bwx = CIA_BUS_USE_BWX;
    149 int	cia_pyxis_force_bwx = CIA_PYXIS_FORCE_BWX;
    150 
    151 static int
    152 ciamatch(device_t parent, cfdata_t match, void *aux)
    153 {
    154 	struct mainbus_attach_args *ma = aux;
    155 
    156 	/* Make sure that we're looking for a CIA. */
    157 	if (strcmp(ma->ma_name, cia_cd.cd_name) != 0)
    158 		return (0);
    159 
    160 	if (ciafound)
    161 		return (0);
    162 
    163 	return (1);
    164 }
    165 
    166 /*
    167  * Set up the chipset's function pointers.
    168  */
    169 void
    170 cia_init(struct cia_config *ccp, int mallocsafe)
    171 {
    172 	int pci_use_bwx = cia_pci_use_bwx;
    173 	int bus_use_bwx = cia_bus_use_bwx;
    174 
    175 	ccp->cc_hae_mem = REGVAL(CIA_CSR_HAE_MEM);
    176 	ccp->cc_hae_io = REGVAL(CIA_CSR_HAE_IO);
    177 	ccp->cc_rev = REGVAL(CIA_CSR_REV) & REV_MASK;
    178 
    179 	/*
    180 	 * Determine if we have a Pyxis.  Only two systypes can
    181 	 * have this: the EB164 systype (AlphaPC164LX and AlphaPC164SX)
    182 	 * and the DEC_550 systype (Miata).
    183 	 */
    184 	if ((cputype == ST_EB164 &&
    185 	     (hwrpb->rpb_variation & SV_ST_MASK) >= SV_ST_ALPHAPC164LX_400) ||
    186 	    cputype == ST_DEC_550) {
    187 		ccp->cc_flags |= CCF_ISPYXIS;
    188 		if (cia_pyxis_force_bwx)
    189 			pci_use_bwx = bus_use_bwx = 1;
    190 	}
    191 
    192 	/*
    193 	 * ALCOR/ALCOR2 Revisions >= 2 and Pyxis have the CNFG register.
    194 	 */
    195 	if (ccp->cc_rev >= 2 || (ccp->cc_flags & CCF_ISPYXIS) != 0)
    196 		ccp->cc_cnfg = REGVAL(CIA_CSR_CNFG);
    197 	else
    198 		ccp->cc_cnfg = 0;
    199 
    200 	/*
    201 	 * Use BWX iff:
    202 	 *
    203 	 *	- It hasn't been disbled by the user,
    204 	 *	- it's enabled in CNFG,
    205 	 *	- we're implementation version ev5,
    206 	 *	- BWX is enabled in the CPU's capabilities mask
    207 	 */
    208 	if ((pci_use_bwx || bus_use_bwx) &&
    209 	    (ccp->cc_cnfg & CNFG_BWEN) != 0 &&
    210 	    (cpu_amask & ALPHA_AMASK_BWX) != 0) {
    211 		uint32_t ctrl;
    212 
    213 		if (pci_use_bwx)
    214 			ccp->cc_flags |= CCF_PCI_USE_BWX;
    215 		if (bus_use_bwx)
    216 			ccp->cc_flags |= CCF_BUS_USE_BWX;
    217 
    218 		/*
    219 		 * For whatever reason, the firmware seems to enable PCI
    220 		 * loopback mode if it also enables BWX.  Make sure it's
    221 		 * enabled if we have an old, buggy firmware rev.
    222 		 */
    223 		alpha_mb();
    224 		ctrl = REGVAL(CIA_CSR_CTRL);
    225 		if ((ctrl & CTRL_PCI_LOOP_EN) == 0) {
    226 			REGVAL(CIA_CSR_CTRL) = ctrl | CTRL_PCI_LOOP_EN;
    227 			alpha_mb();
    228 		}
    229 	}
    230 
    231 	if (!ccp->cc_initted) {
    232 		/* don't do these twice since they set up extents */
    233 		if (ccp->cc_flags & CCF_BUS_USE_BWX) {
    234 			cia_bwx_bus_io_init(&ccp->cc_iot, ccp);
    235 			cia_bwx_bus_mem_init(&ccp->cc_memt, ccp);
    236 
    237 			/*
    238 			 * We have one window for both PCI I/O and MEM
    239 			 * in BWX mode.
    240 			 */
    241 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 1;
    242 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 1;
    243 		} else {
    244 			cia_swiz_bus_io_init(&ccp->cc_iot, ccp);
    245 			cia_swiz_bus_mem_init(&ccp->cc_memt, ccp);
    246 
    247 			/*
    248 			 * We have two I/O windows and 4 MEM windows in
    249 			 * SWIZ mode.
    250 			 */
    251 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 2;
    252 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 4;
    253 		}
    254 		alpha_bus_get_window = cia_bus_get_window;
    255 	}
    256 	ccp->cc_mallocsafe = mallocsafe;
    257 
    258 	cia_pci_init(&ccp->cc_pc, ccp);
    259 	alpha_pci_chipset = &ccp->cc_pc;
    260 
    261 	ccp->cc_initted = 1;
    262 }
    263 
    264 static void
    265 ciaattach(device_t parent, device_t self, void *aux)
    266 {
    267 	struct cia_softc *sc = device_private(self);
    268 	struct cia_config *ccp;
    269 	struct pcibus_attach_args pba;
    270 	char bits[64];
    271 	const char *name;
    272 
    273 	/* note that we've attached the chipset; can't have 2 CIAs. */
    274 	ciafound = 1;
    275 	sc->sc_dev = self;
    276 
    277 	/*
    278 	 * set up the chipset's info; done once at console init time
    279 	 * (maybe), but we must do it here as well to take care of things
    280 	 * that need to use memory allocation.
    281 	 */
    282 	ccp = sc->sc_ccp = &cia_configuration;
    283 	cia_init(ccp, 1);
    284 
    285 	if (ccp->cc_flags & CCF_ISPYXIS) {
    286 		name = "Pyxis";
    287 	} else {
    288 		name = "ALCOR/ALCOR2";
    289 	}
    290 
    291 	aprint_normal(": DECchip 2117x Core Logic Chipset (%s), pass %d\n",
    292 	    name, ccp->cc_rev + 1);
    293 	if (ccp->cc_cnfg) {
    294 		snprintb(bits, sizeof(bits), CIA_CSR_CNFG_BITS, ccp->cc_cnfg);
    295 		aprint_normal_dev(self, "extended capabilities: %s\n", bits);
    296 	}
    297 
    298 	switch (ccp->cc_flags & (CCF_PCI_USE_BWX|CCF_BUS_USE_BWX)) {
    299 	case CCF_PCI_USE_BWX|CCF_BUS_USE_BWX:
    300 		name = "PCI config and bus";
    301 		break;
    302 	case CCF_PCI_USE_BWX:
    303 		name = "PCI config";
    304 		break;
    305 	case CCF_BUS_USE_BWX:
    306 		name = "bus";
    307 		break;
    308 	default:
    309 		name = NULL;
    310 		break;
    311 	}
    312 	if (name != NULL)
    313 		aprint_normal_dev(self, "using BWX for %s access\n", name);
    314 
    315 #ifdef DEC_550
    316 	if (cputype == ST_DEC_550 &&
    317 	    (hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
    318 		/*
    319 		 * Miata 1 systems have a bug: DMA cannot cross
    320 		 * an 8k boundary!  Make sure PCI read prefetching
    321 		 * is disabled on these chips.  Note that secondary
    322 		 * PCI busses don't have this problem, because of
    323 		 * the way PPBs handle PCI read requests.
    324 		 *
    325 		 * In the 21174 Technical Reference Manual, this is
    326 		 * actually documented as "Pyxis Pass 1", but apparently
    327 		 * there are chips that report themselves as "Pass 1"
    328 		 * which do not have the bug!  Miatas with the Cypress
    329 		 * PCI-ISA bridge (i.e. Miata 1.5 and Miata 2) do not
    330 		 * have the bug, so we use this check.
    331 		 *
    332 		 * NOTE: This bug is actually worked around in cia_dma.c,
    333 		 * when direct-mapped DMA maps are created.
    334 		 *
    335 		 * XXX WE NEED TO THINK ABOUT HOW TO HANDLE THIS FOR
    336 		 * XXX SGMAP DMA MAPPINGS!
    337 		 */
    338 		uint32_t ctrl;
    339 
    340 		/* XXX no bets... */
    341 		aprint_error_dev(self,
    342 		    "WARNING: Pyxis pass 1 DMA bug; no bets...\n");
    343 
    344 		ccp->cc_flags |= CCF_PYXISBUG;
    345 
    346 		alpha_mb();
    347 		ctrl = REGVAL(CIA_CSR_CTRL);
    348 		ctrl &= ~(CTRL_RD_TYPE|CTRL_RL_TYPE|CTRL_RM_TYPE);
    349 		REGVAL(CIA_CSR_CTRL) = ctrl;
    350 		alpha_mb();
    351 	}
    352 #endif /* DEC_550 */
    353 
    354 	cia_dma_init(ccp);
    355 
    356 	switch (cputype) {
    357 #ifdef DEC_KN20AA
    358 	case ST_DEC_KN20AA:
    359 		pci_kn20aa_pickintr(ccp);
    360 		break;
    361 #endif
    362 
    363 #ifdef DEC_EB164
    364 	case ST_EB164:
    365 		pci_eb164_pickintr(ccp);
    366 		break;
    367 #endif
    368 
    369 #ifdef DEC_550
    370 	case ST_DEC_550:
    371 		pci_550_pickintr(ccp);
    372 		break;
    373 #endif
    374 
    375 #ifdef DEC_1000A
    376 	case ST_DEC_1000A:
    377 		pci_1000a_pickintr(ccp, &ccp->cc_iot, &ccp->cc_memt,
    378 			&ccp->cc_pc);
    379 		break;
    380 #endif
    381 
    382 #ifdef DEC_1000
    383 	case ST_DEC_1000:
    384 		pci_1000_pickintr(ccp, &ccp->cc_iot, &ccp->cc_memt,
    385 			&ccp->cc_pc);
    386 		break;
    387 #endif
    388 
    389 	default:
    390 		panic("ciaattach: shouldn't be here, really...");
    391 	}
    392 
    393 	pba.pba_iot = &ccp->cc_iot;
    394 	pba.pba_memt = &ccp->cc_memt;
    395 	pba.pba_dmat =
    396 	    alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI);
    397 	pba.pba_dmat64 = NULL;
    398 	pba.pba_pc = &ccp->cc_pc;
    399 	pba.pba_bus = 0;
    400 	pba.pba_bridgetag = NULL;
    401 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
    402 	if ((ccp->cc_flags & CCF_PYXISBUG) == 0)
    403 		pba.pba_flags |= PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
    404 		    PCI_FLAGS_MWI_OKAY;
    405 	config_found(self, &pba, pcibusprint, CFARG_EOL);
    406 }
    407 
    408 static int
    409 cia_bus_get_window(int type, int window,
    410     struct alpha_bus_space_translation *abst)
    411 {
    412 	struct cia_config *ccp = &cia_configuration;
    413 	bus_space_tag_t st;
    414 
    415 	switch (type) {
    416 	case ALPHA_BUS_TYPE_PCI_IO:
    417 		st = &ccp->cc_iot;
    418 		break;
    419 
    420 	case ALPHA_BUS_TYPE_PCI_MEM:
    421 		st = &ccp->cc_memt;
    422 		break;
    423 
    424 	default:
    425 		panic("cia_bus_get_window");
    426 	}
    427 
    428 	return (alpha_bus_space_get_window(st, window, abst));
    429 }
    430 
    431 void
    432 cia_pyxis_intr_enable(int irq, int onoff)
    433 {
    434 	uint64_t imask;
    435 	int s;
    436 
    437 #if 0
    438 	printf("cia_pyxis_intr_enable: %s %d\n",
    439 	    onoff ? "enabling" : "disabling", irq);
    440 #endif
    441 
    442 	s = splhigh();
    443 	alpha_mb();
    444 	imask = REGVAL64(PYXIS_INT_MASK);
    445 	if (onoff)
    446 		imask |= (1UL << irq);
    447 	else
    448 		imask &= ~(1UL << irq);
    449 	REGVAL64(PYXIS_INT_MASK) = imask;
    450 	alpha_mb();
    451 	splx(s);
    452 }
    453