Home | History | Annotate | Line # | Download | only in pci
cia_pci.c revision 1.15.2.2
      1  1.15.2.2  thorpej /* $NetBSD: cia_pci.c,v 1.15.2.2 1997/09/15 22:35:55 thorpej Exp $ */
      2  1.15.2.2  thorpej 
      3  1.15.2.2  thorpej /*
      4  1.15.2.2  thorpej  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  1.15.2.2  thorpej  * All rights reserved.
      6  1.15.2.2  thorpej  *
      7  1.15.2.2  thorpej  * Author: Chris G. Demetriou
      8  1.15.2.2  thorpej  *
      9  1.15.2.2  thorpej  * Permission to use, copy, modify and distribute this software and
     10  1.15.2.2  thorpej  * its documentation is hereby granted, provided that both the copyright
     11  1.15.2.2  thorpej  * notice and this permission notice appear in all copies of the
     12  1.15.2.2  thorpej  * software, derivative works or modified versions, and any portions
     13  1.15.2.2  thorpej  * thereof, and that both notices appear in supporting documentation.
     14  1.15.2.2  thorpej  *
     15  1.15.2.2  thorpej  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.15.2.2  thorpej  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  1.15.2.2  thorpej  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  1.15.2.2  thorpej  *
     19  1.15.2.2  thorpej  * Carnegie Mellon requests users of this software to return to
     20  1.15.2.2  thorpej  *
     21  1.15.2.2  thorpej  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  1.15.2.2  thorpej  *  School of Computer Science
     23  1.15.2.2  thorpej  *  Carnegie Mellon University
     24  1.15.2.2  thorpej  *  Pittsburgh PA 15213-3890
     25  1.15.2.2  thorpej  *
     26  1.15.2.2  thorpej  * any improvements or extensions that they make and grant Carnegie the
     27  1.15.2.2  thorpej  * rights to redistribute these changes.
     28  1.15.2.2  thorpej  */
     29  1.15.2.2  thorpej 
     30  1.15.2.2  thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     31  1.15.2.2  thorpej 
     32  1.15.2.2  thorpej __KERNEL_RCSID(0, "$NetBSD: cia_pci.c,v 1.15.2.2 1997/09/15 22:35:55 thorpej Exp $");
     33  1.15.2.2  thorpej 
     34  1.15.2.2  thorpej #include <sys/param.h>
     35  1.15.2.2  thorpej #include <sys/systm.h>
     36  1.15.2.2  thorpej #include <sys/kernel.h>
     37  1.15.2.2  thorpej #include <sys/device.h>
     38  1.15.2.2  thorpej #include <vm/vm.h>
     39  1.15.2.2  thorpej 
     40  1.15.2.2  thorpej #include <dev/pci/pcireg.h>
     41  1.15.2.2  thorpej #include <dev/pci/pcivar.h>
     42  1.15.2.2  thorpej #include <alpha/pci/ciareg.h>
     43  1.15.2.2  thorpej #include <alpha/pci/ciavar.h>
     44  1.15.2.2  thorpej 
     45  1.15.2.2  thorpej void		cia_attach_hook __P((struct device *, struct device *,
     46  1.15.2.2  thorpej 		    struct pcibus_attach_args *));
     47  1.15.2.2  thorpej int		cia_bus_maxdevs __P((void *, int));
     48  1.15.2.2  thorpej pcitag_t	cia_make_tag __P((void *, int, int, int));
     49  1.15.2.2  thorpej void		cia_decompose_tag __P((void *, pcitag_t, int *, int *,
     50  1.15.2.2  thorpej 		    int *));
     51  1.15.2.2  thorpej pcireg_t	cia_conf_read __P((void *, pcitag_t, int));
     52  1.15.2.2  thorpej void		cia_conf_write __P((void *, pcitag_t, int, pcireg_t));
     53  1.15.2.2  thorpej 
     54  1.15.2.2  thorpej void
     55  1.15.2.2  thorpej cia_pci_init(pc, v)
     56  1.15.2.2  thorpej 	pci_chipset_tag_t pc;
     57  1.15.2.2  thorpej 	void *v;
     58  1.15.2.2  thorpej {
     59  1.15.2.2  thorpej 
     60  1.15.2.2  thorpej 	pc->pc_conf_v = v;
     61  1.15.2.2  thorpej 	pc->pc_attach_hook = cia_attach_hook;
     62  1.15.2.2  thorpej 	pc->pc_bus_maxdevs = cia_bus_maxdevs;
     63  1.15.2.2  thorpej 	pc->pc_make_tag = cia_make_tag;
     64  1.15.2.2  thorpej 	pc->pc_decompose_tag = cia_decompose_tag;
     65  1.15.2.2  thorpej 	pc->pc_conf_read = cia_conf_read;
     66  1.15.2.2  thorpej 	pc->pc_conf_write = cia_conf_write;
     67  1.15.2.2  thorpej }
     68  1.15.2.2  thorpej 
     69  1.15.2.2  thorpej void
     70  1.15.2.2  thorpej cia_attach_hook(parent, self, pba)
     71  1.15.2.2  thorpej 	struct device *parent, *self;
     72  1.15.2.2  thorpej 	struct pcibus_attach_args *pba;
     73  1.15.2.2  thorpej {
     74  1.15.2.2  thorpej }
     75  1.15.2.2  thorpej 
     76  1.15.2.2  thorpej int
     77  1.15.2.2  thorpej cia_bus_maxdevs(cpv, busno)
     78  1.15.2.2  thorpej 	void *cpv;
     79  1.15.2.2  thorpej 	int busno;
     80  1.15.2.2  thorpej {
     81  1.15.2.2  thorpej 
     82  1.15.2.2  thorpej 	return 32;
     83  1.15.2.2  thorpej }
     84  1.15.2.2  thorpej 
     85  1.15.2.2  thorpej pcitag_t
     86  1.15.2.2  thorpej cia_make_tag(cpv, b, d, f)
     87  1.15.2.2  thorpej 	void *cpv;
     88  1.15.2.2  thorpej 	int b, d, f;
     89  1.15.2.2  thorpej {
     90  1.15.2.2  thorpej 
     91  1.15.2.2  thorpej 	return (b << 16) | (d << 11) | (f << 8);
     92  1.15.2.2  thorpej }
     93  1.15.2.2  thorpej 
     94  1.15.2.2  thorpej void
     95  1.15.2.2  thorpej cia_decompose_tag(cpv, tag, bp, dp, fp)
     96  1.15.2.2  thorpej 	void *cpv;
     97  1.15.2.2  thorpej 	pcitag_t tag;
     98  1.15.2.2  thorpej 	int *bp, *dp, *fp;
     99  1.15.2.2  thorpej {
    100  1.15.2.2  thorpej 
    101  1.15.2.2  thorpej 	if (bp != NULL)
    102  1.15.2.2  thorpej 		*bp = (tag >> 16) & 0xff;
    103  1.15.2.2  thorpej 	if (dp != NULL)
    104  1.15.2.2  thorpej 		*dp = (tag >> 11) & 0x1f;
    105  1.15.2.2  thorpej 	if (fp != NULL)
    106  1.15.2.2  thorpej 		*fp = (tag >> 8) & 0x7;
    107  1.15.2.2  thorpej }
    108  1.15.2.2  thorpej 
    109  1.15.2.2  thorpej pcireg_t
    110  1.15.2.2  thorpej cia_conf_read(cpv, tag, offset)
    111  1.15.2.2  thorpej 	void *cpv;
    112  1.15.2.2  thorpej 	pcitag_t tag;
    113  1.15.2.2  thorpej 	int offset;
    114  1.15.2.2  thorpej {
    115  1.15.2.2  thorpej 	struct cia_config *ccp = cpv;
    116  1.15.2.2  thorpej 	pcireg_t *datap, data;
    117  1.15.2.2  thorpej 	int s, secondary, ba;
    118  1.15.2.2  thorpej 	int32_t old_haxr2;					/* XXX */
    119  1.15.2.2  thorpej 
    120  1.15.2.2  thorpej #ifdef DIAGNOSTIC
    121  1.15.2.2  thorpej 	s = 0;					/* XXX gcc -Wuninitialized */
    122  1.15.2.2  thorpej 	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
    123  1.15.2.2  thorpej #endif
    124  1.15.2.2  thorpej 
    125  1.15.2.2  thorpej 	/*
    126  1.15.2.2  thorpej 	 * Some (apparently-common) revisions of EB164 and AlphaStation
    127  1.15.2.2  thorpej 	 * firmware do the Wrong thing with PCI master aborts, which are
    128  1.15.2.2  thorpej 	 * caused by accesing the configuration space of devices that
    129  1.15.2.2  thorpej 	 * don't exist (for example).
    130  1.15.2.2  thorpej 	 *
    131  1.15.2.2  thorpej 	 * To work around this, we clear the CIA error register's PCI
    132  1.15.2.2  thorpej 	 * master abort bit before touching PCI configuration space and
    133  1.15.2.2  thorpej 	 * check it afterwards.  If it indicates a master abort,
    134  1.15.2.2  thorpej 	 * the device wasn't there so we return 0xffffffff.
    135  1.15.2.2  thorpej 	 */
    136  1.15.2.2  thorpej 	/* clear the PCI master abort bit in CIA error register */
    137  1.15.2.2  thorpej 	REGVAL(CIA_CSR_CIA_ERR) = CIA_ERR_RCVD_MAS_ABT;
    138  1.15.2.2  thorpej 	alpha_mb();
    139  1.15.2.2  thorpej 	alpha_pal_draina();
    140  1.15.2.2  thorpej 
    141  1.15.2.2  thorpej 	/* secondary if bus # != 0 */
    142  1.15.2.2  thorpej 	alpha_pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
    143  1.15.2.2  thorpej 	if (secondary) {
    144  1.15.2.2  thorpej 		s = splhigh();
    145  1.15.2.2  thorpej 		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
    146  1.15.2.2  thorpej 		alpha_mb();
    147  1.15.2.2  thorpej 		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
    148  1.15.2.2  thorpej 		alpha_mb();
    149  1.15.2.2  thorpej 	}
    150  1.15.2.2  thorpej 
    151  1.15.2.2  thorpej 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
    152  1.15.2.2  thorpej 	    tag << 5UL |					/* XXX */
    153  1.15.2.2  thorpej 	    (offset & ~0x03) << 5 |				/* XXX */
    154  1.15.2.2  thorpej 	    0 << 5 |						/* XXX */
    155  1.15.2.2  thorpej 	    0x3 << 3);						/* XXX */
    156  1.15.2.2  thorpej 	data = (pcireg_t)-1;
    157  1.15.2.2  thorpej 	if (!(ba = badaddr(datap, sizeof *datap)))
    158  1.15.2.2  thorpej 		data = *datap;
    159  1.15.2.2  thorpej 
    160  1.15.2.2  thorpej 	if (secondary) {
    161  1.15.2.2  thorpej 		alpha_mb();
    162  1.15.2.2  thorpej 		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
    163  1.15.2.2  thorpej 		alpha_mb();
    164  1.15.2.2  thorpej 		splx(s);
    165  1.15.2.2  thorpej 	}
    166  1.15.2.2  thorpej 
    167  1.15.2.2  thorpej 	alpha_pal_draina();
    168  1.15.2.2  thorpej 	/* check CIA error register for PCI master abort */
    169  1.15.2.2  thorpej 	if (REGVAL(CIA_CSR_CIA_ERR) & CIA_ERR_RCVD_MAS_ABT) {
    170  1.15.2.2  thorpej 		ba = 1;
    171  1.15.2.2  thorpej 		data = 0xffffffff;
    172  1.15.2.2  thorpej 	}
    173  1.15.2.2  thorpej 
    174  1.15.2.2  thorpej #if 0
    175  1.15.2.2  thorpej 	printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
    176  1.15.2.2  thorpej 	    data, datap, ba ? " (badaddr)" : "");
    177  1.15.2.2  thorpej #endif
    178  1.15.2.2  thorpej 
    179  1.15.2.2  thorpej 	return data;
    180  1.15.2.2  thorpej }
    181  1.15.2.2  thorpej 
    182  1.15.2.2  thorpej void
    183  1.15.2.2  thorpej cia_conf_write(cpv, tag, offset, data)
    184  1.15.2.2  thorpej 	void *cpv;
    185  1.15.2.2  thorpej 	pcitag_t tag;
    186  1.15.2.2  thorpej 	int offset;
    187  1.15.2.2  thorpej 	pcireg_t data;
    188  1.15.2.2  thorpej {
    189  1.15.2.2  thorpej 	struct cia_config *ccp = cpv;
    190  1.15.2.2  thorpej 	pcireg_t *datap;
    191  1.15.2.2  thorpej 	int s, secondary;
    192  1.15.2.2  thorpej 	int32_t old_haxr2;					/* XXX */
    193  1.15.2.2  thorpej 
    194  1.15.2.2  thorpej #ifdef DIAGNOSTIC
    195  1.15.2.2  thorpej 	s = 0;					/* XXX gcc -Wuninitialized */
    196  1.15.2.2  thorpej 	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
    197  1.15.2.2  thorpej #endif
    198  1.15.2.2  thorpej 
    199  1.15.2.2  thorpej 	/* secondary if bus # != 0 */
    200  1.15.2.2  thorpej 	alpha_pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
    201  1.15.2.2  thorpej 	if (secondary) {
    202  1.15.2.2  thorpej 		s = splhigh();
    203  1.15.2.2  thorpej 		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
    204  1.15.2.2  thorpej 		alpha_mb();
    205  1.15.2.2  thorpej 		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
    206  1.15.2.2  thorpej 		alpha_mb();
    207  1.15.2.2  thorpej 	}
    208  1.15.2.2  thorpej 
    209  1.15.2.2  thorpej 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
    210  1.15.2.2  thorpej 	    tag << 5UL |					/* XXX */
    211  1.15.2.2  thorpej 	    (offset & ~0x03) << 5 |				/* XXX */
    212  1.15.2.2  thorpej 	    0 << 5 |						/* XXX */
    213  1.15.2.2  thorpej 	    0x3 << 3);						/* XXX */
    214  1.15.2.2  thorpej 	*datap = data;
    215  1.15.2.2  thorpej 
    216  1.15.2.2  thorpej 	if (secondary) {
    217  1.15.2.2  thorpej 		alpha_mb();
    218  1.15.2.2  thorpej 		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
    219  1.15.2.2  thorpej 		alpha_mb();
    220  1.15.2.2  thorpej 		splx(s);
    221  1.15.2.2  thorpej 	}
    222  1.15.2.2  thorpej 
    223  1.15.2.2  thorpej #if 0
    224  1.15.2.2  thorpej 	printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
    225  1.15.2.2  thorpej 	    reg, data, datap);
    226  1.15.2.2  thorpej #endif
    227  1.15.2.2  thorpej }
    228