Home | History | Annotate | Line # | Download | only in bonito
bonito_pci.c revision 1.6
      1  1.6     matt /*	$NetBSD: bonito_pci.c,v 1.6 2009/08/06 00:42:22 matt 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  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej /*
     33  1.1  thorpej  * PCI configuration space support for the Algorithmics BONITO
     34  1.1  thorpej  * MIPS PCI and memory controller.
     35  1.1  thorpej  */
     36  1.3    lukem 
     37  1.3    lukem #include <sys/cdefs.h>
     38  1.6     matt __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.6 2009/08/06 00:42:22 matt Exp $");
     39  1.1  thorpej 
     40  1.1  thorpej #include <sys/param.h>
     41  1.1  thorpej #include <sys/systm.h>
     42  1.1  thorpej #include <sys/device.h>
     43  1.1  thorpej 
     44  1.1  thorpej #include <machine/bus.h>
     45  1.1  thorpej #include <machine/intr.h>
     46  1.1  thorpej #include <machine/locore.h>
     47  1.1  thorpej 
     48  1.1  thorpej #include <dev/pci/pcireg.h>
     49  1.1  thorpej #include <dev/pci/pcivar.h>
     50  1.1  thorpej 
     51  1.1  thorpej #include <mips/bonito/bonitoreg.h>
     52  1.1  thorpej #include <mips/bonito/bonitovar.h>
     53  1.1  thorpej 
     54  1.1  thorpej /*
     55  1.1  thorpej  * Bonito systems are always single-processor, so this is sufficient.
     56  1.1  thorpej  */
     57  1.1  thorpej #define	PCI_CONF_LOCK(s)	(s) = splhigh()
     58  1.1  thorpej #define	PCI_CONF_UNLOCK(s)	splx((s))
     59  1.1  thorpej 
     60  1.1  thorpej void		bonito_attach_hook(struct device *, struct device *,
     61  1.1  thorpej 		    struct pcibus_attach_args *);
     62  1.1  thorpej int		bonito_bus_maxdevs(void *, int);
     63  1.1  thorpej pcitag_t	bonito_make_tag(void *, int, int, int);
     64  1.1  thorpej void		bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
     65  1.1  thorpej pcireg_t	bonito_conf_read(void *, pcitag_t, int);
     66  1.1  thorpej void		bonito_conf_write(void *, pcitag_t, int, pcireg_t);
     67  1.1  thorpej 
     68  1.1  thorpej void
     69  1.1  thorpej bonito_pci_init(pci_chipset_tag_t pc, struct bonito_config *bc)
     70  1.1  thorpej {
     71  1.1  thorpej 
     72  1.1  thorpej 	pc->pc_conf_v = bc;
     73  1.1  thorpej 	pc->pc_attach_hook = bonito_attach_hook;
     74  1.1  thorpej 	pc->pc_bus_maxdevs = bonito_bus_maxdevs;
     75  1.1  thorpej 	pc->pc_make_tag = bonito_make_tag;
     76  1.1  thorpej 	pc->pc_decompose_tag = bonito_decompose_tag;
     77  1.1  thorpej 	pc->pc_conf_read = bonito_conf_read;
     78  1.1  thorpej 	pc->pc_conf_write = bonito_conf_write;
     79  1.1  thorpej }
     80  1.1  thorpej 
     81  1.1  thorpej void
     82  1.1  thorpej bonito_attach_hook(struct device *parent, struct device *self,
     83  1.1  thorpej     struct pcibus_attach_args *pba)
     84  1.1  thorpej {
     85  1.1  thorpej }
     86  1.1  thorpej 
     87  1.1  thorpej int
     88  1.1  thorpej bonito_bus_maxdevs(void *v, int busno)
     89  1.1  thorpej {
     90  1.1  thorpej 
     91  1.1  thorpej 	return (32);
     92  1.1  thorpej }
     93  1.1  thorpej 
     94  1.1  thorpej pcitag_t
     95  1.1  thorpej bonito_make_tag(void *v, int b, int d, int f)
     96  1.1  thorpej {
     97  1.1  thorpej 
     98  1.1  thorpej 	return ((b << 16) | (d << 11) | (f << 8));
     99  1.1  thorpej }
    100  1.1  thorpej 
    101  1.1  thorpej void
    102  1.1  thorpej bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    103  1.1  thorpej {
    104  1.1  thorpej 
    105  1.1  thorpej 	if (bp != NULL)
    106  1.1  thorpej 		*bp = (tag >> 16) & 0xff;
    107  1.1  thorpej 	if (dp != NULL)
    108  1.1  thorpej 		*dp = (tag >> 11) & 0x1f;
    109  1.1  thorpej 	if (fp != NULL)
    110  1.1  thorpej 		*fp = (tag >> 8) & 0x7;
    111  1.1  thorpej }
    112  1.1  thorpej 
    113  1.6     matt static bool
    114  1.1  thorpej bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
    115  1.1  thorpej     u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
    116  1.1  thorpej {
    117  1.1  thorpej 	int b, d, f;
    118  1.1  thorpej 
    119  1.1  thorpej 	bonito_decompose_tag(bc, tag, &b, &d, &f);
    120  1.1  thorpej 
    121  1.1  thorpej 	if (b == 0) {
    122  1.1  thorpej 		if (d > (31 - bc->bc_adbase))
    123  1.6     matt 			return true;
    124  1.6     matt 		*cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
    125  1.1  thorpej 		*pcimap_cfg = 0;
    126  1.1  thorpej 	} else {
    127  1.1  thorpej 		*cfgoff = tag | offset;
    128  1.1  thorpej 		*pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
    129  1.1  thorpej 	}
    130  1.1  thorpej 
    131  1.6     matt 	return false;
    132  1.1  thorpej }
    133  1.1  thorpej 
    134  1.1  thorpej pcireg_t
    135  1.1  thorpej bonito_conf_read(void *v, pcitag_t tag, int offset)
    136  1.1  thorpej {
    137  1.1  thorpej 	struct bonito_config *bc = v;
    138  1.1  thorpej 	pcireg_t data;
    139  1.2   simonb 	u_int32_t cfgoff, dummy, pcimap_cfg;
    140  1.1  thorpej 	int s;
    141  1.1  thorpej 
    142  1.1  thorpej 	if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
    143  1.1  thorpej 		return ((pcireg_t) -1);
    144  1.1  thorpej 
    145  1.1  thorpej 	PCI_CONF_LOCK(s);
    146  1.1  thorpej 
    147  1.1  thorpej 	/* clear aborts */
    148  1.1  thorpej 	REGVAL(BONITO_PCICMD) |=
    149  1.1  thorpej 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
    150  1.1  thorpej 
    151  1.1  thorpej 	/* high 16 bits of address go into PciMapCfg register */
    152  1.1  thorpej 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
    153  1.1  thorpej 
    154  1.1  thorpej 	wbflush();
    155  1.2   simonb 	/* Issue a read to make sure the write is posted */
    156  1.2   simonb 	dummy = REGVAL(BONITO_PCIMAP_CFG);
    157  1.1  thorpej 
    158  1.1  thorpej 	/* low 16 bits of address are offset into config space */
    159  1.1  thorpej 	data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
    160  1.1  thorpej 
    161  1.1  thorpej 	/* check for error */
    162  1.1  thorpej 	if (REGVAL(BONITO_PCICMD) &
    163  1.1  thorpej 	    (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
    164  1.1  thorpej 		data = (pcireg_t) -1;
    165  1.1  thorpej 
    166  1.1  thorpej 	PCI_CONF_UNLOCK(s);
    167  1.1  thorpej 
    168  1.1  thorpej 	return (data);
    169  1.1  thorpej }
    170  1.1  thorpej 
    171  1.1  thorpej void
    172  1.1  thorpej bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
    173  1.1  thorpej {
    174  1.1  thorpej 	struct bonito_config *vt = v;
    175  1.2   simonb 	u_int32_t cfgoff, dummy, pcimap_cfg;
    176  1.1  thorpej 	int s;
    177  1.1  thorpej 
    178  1.1  thorpej 	if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
    179  1.1  thorpej 		panic("bonito_conf_write");
    180  1.1  thorpej 
    181  1.1  thorpej 	PCI_CONF_LOCK(s);
    182  1.1  thorpej 
    183  1.1  thorpej 	/* clear aborts */
    184  1.1  thorpej 	REGVAL(BONITO_PCICMD) |=
    185  1.1  thorpej 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
    186  1.1  thorpej 
    187  1.1  thorpej 	/* high 16 bits of address go into PciMapCfg register */
    188  1.1  thorpej 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
    189  1.1  thorpej 
    190  1.1  thorpej 	wbflush();
    191  1.2   simonb 	/* Issue a read to make sure the write is posted */
    192  1.2   simonb 	dummy = REGVAL(BONITO_PCIMAP_CFG);
    193  1.1  thorpej 
    194  1.1  thorpej 	/* low 16 bits of address are offset into config space */
    195  1.1  thorpej 	REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
    196  1.1  thorpej 
    197  1.1  thorpej 	PCI_CONF_UNLOCK(s);
    198  1.1  thorpej }
    199