Home | History | Annotate | Line # | Download | only in pci
cia_pci.c revision 1.10
      1 /* $NetBSD: cia_pci.c,v 1.10 1997/04/07 05:46:49 cgd 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 (at) 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 <machine/options.h>		/* Pull in config options headers */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <vm/vm.h>
     37 
     38 #include <dev/pci/pcireg.h>
     39 #include <dev/pci/pcivar.h>
     40 #include <alpha/pci/ciareg.h>
     41 #include <alpha/pci/ciavar.h>
     42 
     43 #include <machine/rpb.h>	/* XXX for eb164 CIA firmware workarounds. */
     44 
     45 void		cia_attach_hook __P((struct device *, struct device *,
     46 		    struct pcibus_attach_args *));
     47 int		cia_bus_maxdevs __P((void *, int));
     48 pcitag_t	cia_make_tag __P((void *, int, int, int));
     49 void		cia_decompose_tag __P((void *, pcitag_t, int *, int *,
     50 		    int *));
     51 pcireg_t	cia_conf_read __P((void *, pcitag_t, int));
     52 void		cia_conf_write __P((void *, pcitag_t, int, pcireg_t));
     53 
     54 void
     55 cia_pci_init(pc, v)
     56 	pci_chipset_tag_t pc;
     57 	void *v;
     58 {
     59 
     60 	pc->pc_conf_v = v;
     61 	pc->pc_attach_hook = cia_attach_hook;
     62 	pc->pc_bus_maxdevs = cia_bus_maxdevs;
     63 	pc->pc_make_tag = cia_make_tag;
     64 	pc->pc_decompose_tag = cia_decompose_tag;
     65 	pc->pc_conf_read = cia_conf_read;
     66 	pc->pc_conf_write = cia_conf_write;
     67 }
     68 
     69 void
     70 cia_attach_hook(parent, self, pba)
     71 	struct device *parent, *self;
     72 	struct pcibus_attach_args *pba;
     73 {
     74 }
     75 
     76 int
     77 cia_bus_maxdevs(cpv, busno)
     78 	void *cpv;
     79 	int busno;
     80 {
     81 
     82 	return 32;
     83 }
     84 
     85 pcitag_t
     86 cia_make_tag(cpv, b, d, f)
     87 	void *cpv;
     88 	int b, d, f;
     89 {
     90 
     91 	return (b << 16) | (d << 11) | (f << 8);
     92 }
     93 
     94 void
     95 cia_decompose_tag(cpv, tag, bp, dp, fp)
     96 	void *cpv;
     97 	pcitag_t tag;
     98 	int *bp, *dp, *fp;
     99 {
    100 
    101 	if (bp != NULL)
    102 		*bp = (tag >> 16) & 0xff;
    103 	if (dp != NULL)
    104 		*dp = (tag >> 11) & 0x1f;
    105 	if (fp != NULL)
    106 		*fp = (tag >> 8) & 0x7;
    107 }
    108 
    109 pcireg_t
    110 cia_conf_read(cpv, tag, offset)
    111 	void *cpv;
    112 	pcitag_t tag;
    113 	int offset;
    114 {
    115 	struct cia_config *ccp = cpv;
    116 	pcireg_t *datap, data;
    117 	int s, secondary, ba;
    118 	int32_t old_haxr2;					/* XXX */
    119 #ifdef DEC_EB164
    120 	extern int cputype;					/* XXX */
    121 #endif
    122 
    123 #ifdef DIAGNOSTIC
    124 	s = 0;					/* XXX gcc -Wuninitialized */
    125 	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
    126 #endif
    127 
    128 #ifdef DEC_EB164
    129 	/*
    130 	 * Some (apparently-common) revisions of EB164 firmware do the
    131 	 * Wrong thing with PCI master aborts, which are caused by
    132 	 * accesing the configuration space of devices that don't
    133 	 * exist (for example).
    134 	 *
    135 	 * On EB164's we clear the CIA error register's PCI master
    136 	 * abort bit before touching PCI configuration space and
    137 	 * check it afterwards.  If it indicates a master abort,
    138 	 * the device wasn't there so we return 0xffffffff.
    139 	 */
    140 	if (cputype == ST_EB164) {
    141 		/* clear the PCI master abort bit in CIA error register */
    142 		REGVAL(CIA_CSR_CIA_ERR) = 0x00000080;		/* XXX */
    143 		alpha_mb();
    144 		alpha_pal_draina();
    145 	}
    146 #endif
    147 
    148 	/* secondary if bus # != 0 */
    149 	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
    150 	if (secondary) {
    151 		s = splhigh();
    152 		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
    153 		alpha_mb();
    154 		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
    155 		alpha_mb();
    156 	}
    157 
    158 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
    159 	    tag << 5UL |					/* XXX */
    160 	    (offset & ~0x03) << 5 |				/* XXX */
    161 	    0 << 5 |						/* XXX */
    162 	    0x3 << 3);						/* XXX */
    163 	data = (pcireg_t)-1;
    164 	if (!(ba = badaddr(datap, sizeof *datap)))
    165 		data = *datap;
    166 
    167 	if (secondary) {
    168 		alpha_mb();
    169 		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
    170 		alpha_mb();
    171 		splx(s);
    172 	}
    173 
    174 #ifdef DEC_EB164
    175 	if (cputype == ST_EB164) {
    176 		alpha_pal_draina();
    177 		/* check CIA error register for PCI master abort */
    178 		if (REGVAL(CIA_CSR_CIA_ERR) & 0x00000080) {	/* XXX */
    179 			ba = 1;
    180 			data = 0xffffffff;
    181 		}
    182 	}
    183 #endif
    184 
    185 #if 0
    186 	printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
    187 	    data, datap, ba ? " (badaddr)" : "");
    188 #endif
    189 
    190 	return data;
    191 }
    192 
    193 void
    194 cia_conf_write(cpv, tag, offset, data)
    195 	void *cpv;
    196 	pcitag_t tag;
    197 	int offset;
    198 	pcireg_t data;
    199 {
    200 	struct cia_config *ccp = cpv;
    201 	pcireg_t *datap;
    202 	int s, secondary;
    203 	int32_t old_haxr2;					/* XXX */
    204 
    205 #ifdef DIAGNOSTIC
    206 	s = 0;					/* XXX gcc -Wuninitialized */
    207 	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
    208 #endif
    209 
    210 	/* secondary if bus # != 0 */
    211 	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
    212 	if (secondary) {
    213 		s = splhigh();
    214 		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
    215 		alpha_mb();
    216 		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
    217 		alpha_mb();
    218 	}
    219 
    220 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
    221 	    tag << 5UL |					/* XXX */
    222 	    (offset & ~0x03) << 5 |				/* XXX */
    223 	    0 << 5 |						/* XXX */
    224 	    0x3 << 3);						/* XXX */
    225 	*datap = data;
    226 
    227 	if (secondary) {
    228 		alpha_mb();
    229 		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
    230 		alpha_mb();
    231 		splx(s);
    232 	}
    233 
    234 #if 0
    235 	printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
    236 	    reg, data, datap);
    237 #endif
    238 }
    239