Home | History | Annotate | Line # | Download | only in g2
gapspci_pci.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: gapspci_pci.c,v 1.4 2002/05/15 17:09:04 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2001 Marcus Comstedt.
      5  1.1  thorpej  * Copyright (c) 2001 Jason R. Thorpe.
      6  1.1  thorpej  * All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      9  1.1  thorpej  * modification, are permitted provided that the following conditions
     10  1.1  thorpej  * are met:
     11  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     12  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     13  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     16  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     17  1.1  thorpej  *    must display the following acknowledgement:
     18  1.1  thorpej  *	This product includes software developed by Marcus Comstedt.
     19  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     21  1.1  thorpej  *    from this software without specific prior written permission.
     22  1.1  thorpej  *
     23  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  thorpej  */
     35  1.1  thorpej 
     36  1.1  thorpej /*
     37  1.1  thorpej  * PCI configuraiton space implementation for the SEGA GAPS PCI bridge.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     41  1.1  thorpej 
     42  1.1  thorpej #include <sys/param.h>
     43  1.1  thorpej #include <sys/systm.h>
     44  1.1  thorpej #include <sys/kernel.h>
     45  1.1  thorpej #include <sys/device.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <machine/cpu.h>
     48  1.1  thorpej #include <machine/bus.h>
     49  1.2   marcus #include <machine/sysasicvar.h>
     50  1.1  thorpej 
     51  1.1  thorpej #include <dev/pci/pcivar.h>
     52  1.1  thorpej #include <dev/pci/pcireg.h>
     53  1.1  thorpej 
     54  1.1  thorpej #include <dreamcast/dev/g2/gapspcivar.h>
     55  1.1  thorpej 
     56  1.1  thorpej void		gaps_attach_hook(struct device *, struct device *,
     57  1.1  thorpej 		    struct pcibus_attach_args *);
     58  1.1  thorpej int		gaps_bus_maxdevs(void *, int);
     59  1.1  thorpej pcitag_t	gaps_make_tag(void *, int, int, int);
     60  1.4  thorpej void		gaps_decompose_tag(void *, pcitag_t, int *, int *, int *);
     61  1.1  thorpej pcireg_t	gaps_conf_read(void *, pcitag_t, int);
     62  1.1  thorpej void		gaps_conf_write(void *, pcitag_t, int, pcireg_t);
     63  1.1  thorpej 
     64  1.1  thorpej int		gaps_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
     65  1.1  thorpej const char	*gaps_intr_string(void *, pci_intr_handle_t);
     66  1.1  thorpej void		*gaps_intr_establish(void *, pci_intr_handle_t,
     67  1.1  thorpej 		    int, int (*)(void *), void *);
     68  1.1  thorpej void		gaps_intr_disestablish(void *, void *);
     69  1.1  thorpej 
     70  1.1  thorpej void
     71  1.1  thorpej gaps_pci_init(struct gaps_softc *sc)
     72  1.1  thorpej {
     73  1.1  thorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
     74  1.1  thorpej 
     75  1.1  thorpej 	memset(pc, 0, sizeof(*pc));
     76  1.1  thorpej 
     77  1.1  thorpej 	pc->pc_attach_hook = gaps_attach_hook;
     78  1.1  thorpej 	pc->pc_bus_maxdevs = gaps_bus_maxdevs;
     79  1.1  thorpej 	pc->pc_make_tag = gaps_make_tag;
     80  1.4  thorpej 	pc->pc_decompose_tag = gaps_decompose_tag;
     81  1.1  thorpej 	pc->pc_conf_read = gaps_conf_read;
     82  1.1  thorpej 	pc->pc_conf_write = gaps_conf_write;
     83  1.1  thorpej 	pc->pc_conf_v = sc;
     84  1.1  thorpej 
     85  1.1  thorpej 	pc->pc_intr_map = gaps_intr_map;
     86  1.1  thorpej 	pc->pc_intr_string = gaps_intr_string;
     87  1.1  thorpej 	pc->pc_intr_establish = gaps_intr_establish;
     88  1.1  thorpej 	pc->pc_intr_disestablish = gaps_intr_disestablish;
     89  1.1  thorpej 
     90  1.1  thorpej 	if (bus_space_map(sc->sc_memt, 0x01001600, 0x100,
     91  1.1  thorpej 	    0, &sc->sc_pci_memh) != 0)
     92  1.1  thorpej 		panic("gaps_pci_init: can't map PCI configuration space");
     93  1.1  thorpej }
     94  1.1  thorpej 
     95  1.1  thorpej #define	GAPS_PCITAG_MAGIC	0x022473
     96  1.1  thorpej 
     97  1.1  thorpej void
     98  1.1  thorpej gaps_attach_hook(struct device *bus, struct device *pci,
     99  1.1  thorpej     struct pcibus_attach_args *pba)
    100  1.1  thorpej {
    101  1.1  thorpej 	struct gaps_softc *sc = (void *) bus;
    102  1.1  thorpej 
    103  1.1  thorpej 	/*
    104  1.1  thorpej 	 * Now that we know there's a bus configured, go ahead and
    105  1.1  thorpej 	 * program the BAR on the device.
    106  1.1  thorpej 	 */
    107  1.1  thorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC,
    108  1.1  thorpej 	    PCI_MAPREG_START + 4, 0x01000000);
    109  1.1  thorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC, PCI_COMMAND_STATUS_REG,
    110  1.1  thorpej 	    pci_conf_read(&sc->sc_pc, 0, PCI_COMMAND_STATUS_REG) |
    111  1.1  thorpej 	    PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    112  1.1  thorpej }
    113  1.1  thorpej 
    114  1.1  thorpej int
    115  1.1  thorpej gaps_bus_maxdevs(void *v, int bus)
    116  1.1  thorpej {
    117  1.1  thorpej 
    118  1.1  thorpej 	return (1);
    119  1.1  thorpej }
    120  1.1  thorpej 
    121  1.1  thorpej pcitag_t
    122  1.1  thorpej gaps_make_tag(void *v, int bus, int dev, int func)
    123  1.1  thorpej {
    124  1.1  thorpej 
    125  1.1  thorpej 	if (bus == 0 && dev == 0 && func == 0)
    126  1.1  thorpej 		return (GAPS_PCITAG_MAGIC);
    127  1.1  thorpej 
    128  1.1  thorpej 	return (0);
    129  1.4  thorpej }
    130  1.4  thorpej 
    131  1.4  thorpej void
    132  1.4  thorpej gaps_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    133  1.4  thorpej {
    134  1.4  thorpej 	int b, d, f;
    135  1.4  thorpej 
    136  1.4  thorpej 	if (tag == GAPS_PCITAG_MAGIC)
    137  1.4  thorpej 		b = d = f = 0;
    138  1.4  thorpej 	else {
    139  1.4  thorpej 		/*
    140  1.4  thorpej 		 * Invalid for GAPS.  These values ensure that a valid
    141  1.4  thorpej 		 * tag cannot be built.
    142  1.4  thorpej 		 */
    143  1.4  thorpej 		b = 0xff;
    144  1.4  thorpej 		d = 0x1f;
    145  1.4  thorpej 		f = 0x7;
    146  1.4  thorpej 	}
    147  1.4  thorpej 
    148  1.4  thorpej 	if (bp != NULL)
    149  1.4  thorpej 		*bp = b;
    150  1.4  thorpej 	if (dp != NULL)
    151  1.4  thorpej 		*dp = d;
    152  1.4  thorpej 	if (fp != NULL)
    153  1.4  thorpej 		*fp = f;
    154  1.1  thorpej }
    155  1.1  thorpej 
    156  1.1  thorpej pcireg_t
    157  1.1  thorpej gaps_conf_read(void *v, pcitag_t tag, int reg)
    158  1.1  thorpej {
    159  1.1  thorpej 	struct gaps_softc *sc = v;
    160  1.1  thorpej 
    161  1.1  thorpej 	if (tag != GAPS_PCITAG_MAGIC)
    162  1.1  thorpej 		return (-1);
    163  1.1  thorpej 
    164  1.1  thorpej 	if (reg == (PCI_MAPREG_START + 4)) {
    165  1.1  thorpej 		/*
    166  1.1  thorpej 		 * We fake the BAR -- just return the physical address
    167  1.1  thorpej 		 * to which the device is mapped.
    168  1.1  thorpej 		 */
    169  1.1  thorpej 		return (0x01001700);
    170  1.1  thorpej 	}
    171  1.1  thorpej 
    172  1.1  thorpej 	return (bus_space_read_4(sc->sc_memt, sc->sc_pci_memh, reg));
    173  1.1  thorpej }
    174  1.1  thorpej 
    175  1.1  thorpej void
    176  1.1  thorpej gaps_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
    177  1.1  thorpej {
    178  1.1  thorpej 	struct gaps_softc *sc = v;
    179  1.1  thorpej 
    180  1.1  thorpej 	if (tag != GAPS_PCITAG_MAGIC)
    181  1.1  thorpej 		return;
    182  1.1  thorpej 
    183  1.1  thorpej 	/* Disallow writing to the "BAR" ... it doesn't actually exist. */
    184  1.1  thorpej 	if (reg == (PCI_MAPREG_START + 4) && val != 0x01000000)
    185  1.1  thorpej 		return;
    186  1.1  thorpej 
    187  1.1  thorpej 	bus_space_write_4(sc->sc_memt, sc->sc_pci_memh, reg, val);
    188  1.1  thorpej }
    189  1.1  thorpej 
    190  1.1  thorpej int
    191  1.1  thorpej gaps_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    192  1.1  thorpej {
    193  1.1  thorpej 
    194  1.3      uch 	*ihp = SYSASIC_EVENT_EXT;
    195  1.1  thorpej 	return (0);
    196  1.1  thorpej }
    197  1.1  thorpej 
    198  1.1  thorpej const char *
    199  1.1  thorpej gaps_intr_string(void *v, pci_intr_handle_t ih)
    200  1.1  thorpej {
    201  1.1  thorpej 
    202  1.3      uch 	return ("SH4 IRL 11");
    203  1.1  thorpej }
    204  1.1  thorpej 
    205  1.1  thorpej void *
    206  1.1  thorpej gaps_intr_establish(void *v, pci_intr_handle_t ih, int level,
    207  1.1  thorpej     int (*func)(void *), void *arg)
    208  1.1  thorpej {
    209  1.3      uch 
    210  1.3      uch 	return (sysasic_intr_establish(ih, func, arg));
    211  1.1  thorpej }
    212  1.1  thorpej 
    213  1.1  thorpej void
    214  1.1  thorpej gaps_intr_disestablish(void *v, void *ih)
    215  1.1  thorpej {
    216  1.1  thorpej 
    217  1.1  thorpej 	panic("gaps_intr_disestablish: not implemented");
    218  1.1  thorpej }
    219