Home | History | Annotate | Line # | Download | only in pci
vtpbc.c revision 1.3.24.3
      1  1.3.24.3    skrll /*	$NetBSD: vtpbc.c,v 1.3.24.3 2004/09/21 13:11:36 skrll Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*-
      4       1.1  thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5       1.1  thorpej  * All rights reserved.
      6       1.1  thorpej  *
      7       1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  thorpej  * by Jason R. Thorpe.
      9       1.1  thorpej  *
     10       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.1  thorpej  * modification, are permitted provided that the following conditions
     12       1.1  thorpej  * are met:
     13       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.1  thorpej  *    must display the following acknowledgement:
     20       1.1  thorpej  *	This product includes software developed by the NetBSD
     21       1.1  thorpej  *	Foundation, Inc. and its contributors.
     22       1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24       1.1  thorpej  *    from this software without specific prior written permission.
     25       1.1  thorpej  *
     26       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  thorpej  */
     38       1.1  thorpej 
     39       1.1  thorpej /*
     40       1.1  thorpej  * Support for the V3 Semiconductor i960 PCI bus controller.  This appears
     41       1.1  thorpej  * on some MIPS boards (notably Algorithmics P-4032 and P-5064).
     42       1.1  thorpej  *
     43       1.1  thorpej  * Some help was provided by the Algorithmics PMON sources.
     44       1.1  thorpej  */
     45       1.1  thorpej 
     46  1.3.24.1    skrll #include <sys/cdefs.h>
     47  1.3.24.3    skrll __KERNEL_RCSID(0, "$NetBSD: vtpbc.c,v 1.3.24.3 2004/09/21 13:11:36 skrll Exp $");
     48  1.3.24.1    skrll 
     49       1.1  thorpej #include <sys/param.h>
     50       1.1  thorpej #include <sys/systm.h>
     51       1.1  thorpej #include <sys/device.h>
     52       1.1  thorpej 
     53       1.1  thorpej #include <machine/bus.h>
     54       1.1  thorpej #include <machine/intr.h>
     55       1.1  thorpej #include <machine/locore.h>
     56       1.1  thorpej 
     57       1.1  thorpej #include <dev/pci/pcireg.h>
     58       1.1  thorpej #include <dev/pci/pcivar.h>
     59       1.1  thorpej 
     60       1.1  thorpej #include <algor/pci/vtpbcreg.h>
     61       1.1  thorpej #include <algor/pci/vtpbcvar.h>
     62       1.1  thorpej 
     63       1.1  thorpej struct vtpbc_config vtpbc_configuration;
     64       1.1  thorpej 
     65       1.1  thorpej #define	PCI_CONF_LOCK(s)	(s) = splhigh()
     66       1.1  thorpej #define	PCI_CONF_UNLOCK(s)	splx((s))
     67       1.1  thorpej 
     68       1.1  thorpej const char *vtpbc_revs[] = {
     69       1.1  thorpej 	"A",
     70       1.1  thorpej 	"B0",
     71       1.1  thorpej 	"B1",
     72       1.1  thorpej 	"B2",
     73       1.1  thorpej 	"C0",
     74       1.1  thorpej };
     75       1.1  thorpej const int vtpbc_nrevs = sizeof(vtpbc_revs) / sizeof(vtpbc_revs[0]);
     76       1.1  thorpej 
     77       1.1  thorpej void	vtpbc_attach_hook(struct device *, struct device *,
     78       1.1  thorpej 	    struct pcibus_attach_args *);
     79       1.1  thorpej int	vtpbc_bus_maxdevs(void *, int);
     80       1.1  thorpej pcitag_t vtpbc_make_tag(void *, int, int, int);
     81       1.1  thorpej void	vtpbc_decompose_tag(void *, pcitag_t, int *, int *, int *);
     82       1.1  thorpej pcireg_t vtpbc_conf_read(void *, pcitag_t, int);
     83       1.1  thorpej void	vtpbc_conf_write(void *, pcitag_t, int, pcireg_t);
     84       1.1  thorpej 
     85       1.1  thorpej /*
     86       1.1  thorpej  * vtpbc_init:
     87       1.1  thorpej  *
     88       1.1  thorpej  *	Initialize the V3 PCI controller's software state.  We
     89       1.1  thorpej  *	simply use the existing windows that the firmware has
     90       1.1  thorpej  *	set up for us.
     91       1.1  thorpej  */
     92       1.1  thorpej void
     93       1.1  thorpej vtpbc_init(pci_chipset_tag_t pc, struct vtpbc_config *vt)
     94       1.1  thorpej {
     95       1.1  thorpej 
     96       1.1  thorpej 	pc->pc_conf_v = vt;
     97       1.1  thorpej 	pc->pc_attach_hook = vtpbc_attach_hook;
     98       1.1  thorpej 	pc->pc_bus_maxdevs = vtpbc_bus_maxdevs;
     99       1.1  thorpej 	pc->pc_make_tag = vtpbc_make_tag;
    100       1.1  thorpej 	pc->pc_decompose_tag = vtpbc_decompose_tag;
    101       1.1  thorpej 	pc->pc_conf_read = vtpbc_conf_read;
    102       1.1  thorpej 	pc->pc_conf_write = vtpbc_conf_write;
    103       1.1  thorpej 
    104       1.1  thorpej 	vt->vt_rev = V96X_PCI_CC_REV(vt) & V96X_PCI_CC_REV_VREV;
    105       1.3  thorpej 
    106       1.3  thorpej 	/*
    107       1.3  thorpej 	 * Determine the PCI I/O space base that our PCI
    108       1.3  thorpej 	 * I/O window maps to.  NOTE: We disable this on
    109       1.3  thorpej 	 * PBC rev < B2.
    110       1.3  thorpej 	 *
    111       1.3  thorpej 	 * Also note that PMON has disabled the I/O space
    112       1.3  thorpej 	 * if the old-style PCI address map is in-use.
    113       1.3  thorpej 	 */
    114       1.3  thorpej 	if (vt->vt_rev < V96X_VREV_B2)
    115       1.3  thorpej 		vt->vt_pci_iobase = (bus_addr_t) -1;
    116       1.3  thorpej 	else {
    117       1.3  thorpej 		if ((V96X_LB_BASE2(vt) & V96X_LB_BASEx_ENABLE) == 0)
    118       1.3  thorpej 			vt->vt_pci_iobase = (bus_addr_t) -1;
    119       1.3  thorpej 		else
    120       1.3  thorpej 			vt->vt_pci_iobase =
    121       1.3  thorpej 			    (V96X_LB_MAP2(vt) & V96X_LB_MAPx_MAP_ADR) << 16;
    122       1.3  thorpej 	}
    123       1.2  thorpej 
    124       1.2  thorpej 	/*
    125       1.2  thorpej 	 * Determine the PCI memory space base that our PCI
    126       1.2  thorpej 	 * memory window maps to.
    127       1.2  thorpej 	 */
    128       1.2  thorpej 	vt->vt_pci_membase = (V96X_LB_MAP1(vt) & V96X_LB_MAPx_MAP_ADR) << 16;
    129       1.2  thorpej 
    130       1.2  thorpej 	/*
    131       1.2  thorpej 	 * Determine the PCI window base that maps host RAM for
    132       1.2  thorpej 	 * DMA.
    133       1.2  thorpej 	 */
    134       1.2  thorpej 	vt->vt_dma_winbase = V96X_PCI_BASE1(vt) & 0xfffffff0;
    135       1.1  thorpej }
    136       1.1  thorpej 
    137       1.1  thorpej void
    138       1.1  thorpej vtpbc_attach_hook(struct device *parent, struct device *self,
    139       1.1  thorpej     struct pcibus_attach_args *pba)
    140       1.1  thorpej {
    141       1.1  thorpej }
    142       1.1  thorpej 
    143       1.1  thorpej int
    144       1.1  thorpej vtpbc_bus_maxdevs(void *v, int busno)
    145       1.1  thorpej {
    146       1.1  thorpej 
    147       1.1  thorpej 	return (32);
    148       1.1  thorpej }
    149       1.1  thorpej 
    150       1.1  thorpej pcitag_t
    151       1.1  thorpej vtpbc_make_tag(void *v, int b, int d, int f)
    152       1.1  thorpej {
    153       1.1  thorpej 
    154       1.1  thorpej 	return ((b << 16) | (d << 11) | (f << 8));
    155       1.1  thorpej }
    156       1.1  thorpej 
    157       1.1  thorpej void
    158       1.1  thorpej vtpbc_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    159       1.1  thorpej {
    160       1.1  thorpej 
    161       1.1  thorpej 	if (bp != NULL)
    162       1.1  thorpej 		*bp = (tag >> 16) & 0xff;
    163       1.1  thorpej 	if (dp != NULL)
    164       1.1  thorpej 		*dp = (tag >> 11) & 0x1f;
    165       1.1  thorpej 	if (fp != NULL)
    166       1.1  thorpej 		*fp = (tag >> 8) & 0x7;
    167       1.1  thorpej }
    168       1.1  thorpej 
    169       1.1  thorpej static int
    170       1.1  thorpej vtpbc_conf_addr(struct vtpbc_config *vt, pcitag_t tag, int offset,
    171       1.1  thorpej     u_int32_t *cfgoff, u_int32_t *ad_low)
    172       1.1  thorpej {
    173       1.1  thorpej 	int b, d, f;
    174       1.1  thorpej 
    175       1.1  thorpej 	vtpbc_decompose_tag(vt, tag, &b, &d, &f);
    176       1.1  thorpej 
    177       1.1  thorpej 	if (b == 0) {
    178       1.1  thorpej 		if (d > (31 - vt->vt_adbase))
    179       1.1  thorpej 			return (1);
    180       1.1  thorpej 		*cfgoff = (1UL << (d + vt->vt_adbase)) | (f << 8) |
    181       1.1  thorpej 		    offset;
    182       1.1  thorpej 		*ad_low = 0;
    183       1.1  thorpej 	} else if (vt->vt_rev >= V96X_VREV_C0) {
    184       1.1  thorpej 		*cfgoff = tag | offset;
    185       1.1  thorpej 		*ad_low = V96X_LB_MAPx_AD_LOW_EN;
    186       1.1  thorpej 	} else
    187       1.1  thorpej 		return (1);
    188       1.1  thorpej 
    189       1.1  thorpej 	return (0);
    190       1.1  thorpej }
    191       1.1  thorpej 
    192       1.1  thorpej pcireg_t
    193       1.1  thorpej vtpbc_conf_read(void *v, pcitag_t tag, int offset)
    194       1.1  thorpej {
    195       1.1  thorpej 	struct vtpbc_config *vt = v;
    196       1.1  thorpej 	pcireg_t data;
    197       1.1  thorpej 	u_int32_t cfgoff, ad_low;
    198       1.1  thorpej 	int s;
    199       1.1  thorpej 	u_int16_t errbits;
    200       1.1  thorpej 
    201       1.1  thorpej 	if (vtpbc_conf_addr(vt, tag, offset, &cfgoff, &ad_low))
    202       1.1  thorpej 		return ((pcireg_t) -1);
    203       1.1  thorpej 
    204       1.1  thorpej 	PCI_CONF_LOCK(s);
    205       1.1  thorpej 
    206       1.1  thorpej 	/* high 12 bits of address go into map register */
    207       1.1  thorpej 	V96X_LB_MAP0(vt) = ((cfgoff >> 16) & V96X_LB_MAPx_MAP_ADR) |
    208       1.1  thorpej 	    ad_low | V96X_LB_TYPE_CONF;
    209       1.1  thorpej 
    210       1.1  thorpej 	/* clear aborts */
    211       1.1  thorpej 	V96X_PCI_STAT(vt) |= V96X_PCI_STAT_M_ABORT | V96X_PCI_STAT_T_ABORT;
    212       1.1  thorpej 
    213       1.1  thorpej 	wbflush();
    214       1.1  thorpej 
    215       1.1  thorpej 	/* low 20 bits of address are offset into config space */
    216       1.1  thorpej 	data = *(__volatile u_int32_t *) (vt->vt_cfgbase + (cfgoff & 0xfffff));
    217       1.1  thorpej 
    218       1.1  thorpej 	errbits = V96X_PCI_STAT(vt) &
    219       1.1  thorpej 	    (V96X_PCI_STAT_M_ABORT|V96X_PCI_STAT_T_ABORT);
    220       1.1  thorpej 	if (errbits) {
    221       1.1  thorpej 		V96X_PCI_STAT(vt) |= errbits;
    222       1.1  thorpej 		data = (pcireg_t) -1;
    223       1.1  thorpej 	}
    224       1.1  thorpej 
    225       1.1  thorpej 	PCI_CONF_UNLOCK(s);
    226       1.1  thorpej 
    227       1.1  thorpej 	return (data);
    228       1.1  thorpej }
    229       1.1  thorpej 
    230       1.1  thorpej void
    231       1.1  thorpej vtpbc_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
    232       1.1  thorpej {
    233       1.1  thorpej 	struct vtpbc_config *vt = v;
    234       1.1  thorpej 	u_int32_t cfgoff, ad_low;
    235       1.1  thorpej 	int s;
    236       1.1  thorpej 
    237       1.1  thorpej 	if (vtpbc_conf_addr(vt, tag, offset, &cfgoff, &ad_low))
    238       1.1  thorpej 		panic("vtpbc_conf_write");
    239       1.1  thorpej 
    240       1.1  thorpej 	PCI_CONF_LOCK(s);
    241       1.1  thorpej 
    242       1.1  thorpej 	/* high 12 bits of address go into map register */
    243       1.1  thorpej 	V96X_LB_MAP0(vt) = ((cfgoff >> 16) & V96X_LB_MAPx_MAP_ADR) |
    244       1.1  thorpej 	    ad_low | V96X_LB_TYPE_CONF;
    245       1.1  thorpej 
    246       1.1  thorpej 	/* clear aborts */
    247       1.1  thorpej 	V96X_PCI_STAT(vt) |= V96X_PCI_STAT_M_ABORT | V96X_PCI_STAT_T_ABORT;
    248       1.1  thorpej 
    249       1.1  thorpej 	wbflush();
    250       1.1  thorpej 
    251       1.1  thorpej 	/* low 20 bits of address are offset into config space */
    252       1.1  thorpej 	*(__volatile u_int32_t *) (vt->vt_cfgbase + (cfgoff & 0xfffff)) = data;
    253       1.1  thorpej 
    254       1.1  thorpej 	/* wait for FIFO to drain */
    255       1.1  thorpej 	while (V96X_FIFO_STAT(vt) & V96X_FIFO_STAT_L2P_WR)
    256       1.1  thorpej 		/* spin */ ;
    257       1.1  thorpej 
    258       1.1  thorpej 	PCI_CONF_UNLOCK(s);
    259       1.1  thorpej }
    260