Home | History | Annotate | Line # | Download | only in g2
gapspci_pci.c revision 1.10
      1  1.10  tsutsui /*	$NetBSD: gapspci_pci.c,v 1.10 2007/03/12 14:03:47 tsutsui 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.10  tsutsui __KERNEL_RCSID(0, "$NetBSD: gapspci_pci.c,v 1.10 2007/03/12 14:03:47 tsutsui Exp $");
     42   1.1  thorpej 
     43   1.1  thorpej #include <sys/param.h>
     44   1.1  thorpej #include <sys/systm.h>
     45   1.1  thorpej #include <sys/kernel.h>
     46   1.9  tsutsui #include <sys/device.h>
     47   1.9  tsutsui 
     48   1.1  thorpej #include <machine/cpu.h>
     49   1.1  thorpej #include <machine/bus.h>
     50   1.2   marcus #include <machine/sysasicvar.h>
     51   1.1  thorpej 
     52   1.1  thorpej #include <dev/pci/pcivar.h>
     53   1.1  thorpej #include <dev/pci/pcireg.h>
     54   1.1  thorpej 
     55   1.1  thorpej #include <dreamcast/dev/g2/gapspcivar.h>
     56   1.9  tsutsui 
     57   1.1  thorpej void		gaps_attach_hook(struct device *, struct device *,
     58   1.1  thorpej 		    struct pcibus_attach_args *);
     59   1.1  thorpej int		gaps_bus_maxdevs(void *, int);
     60   1.1  thorpej pcitag_t	gaps_make_tag(void *, int, int, int);
     61   1.4  thorpej void		gaps_decompose_tag(void *, pcitag_t, int *, int *, int *);
     62   1.1  thorpej pcireg_t	gaps_conf_read(void *, pcitag_t, int);
     63   1.1  thorpej void		gaps_conf_write(void *, pcitag_t, int, pcireg_t);
     64   1.1  thorpej 
     65   1.1  thorpej int		gaps_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
     66   1.1  thorpej const char	*gaps_intr_string(void *, pci_intr_handle_t);
     67   1.1  thorpej void		*gaps_intr_establish(void *, pci_intr_handle_t,
     68   1.1  thorpej 		    int, int (*)(void *), void *);
     69   1.1  thorpej void		gaps_intr_disestablish(void *, void *);
     70   1.1  thorpej 
     71   1.1  thorpej void
     72   1.1  thorpej gaps_pci_init(struct gaps_softc *sc)
     73   1.1  thorpej {
     74   1.1  thorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
     75   1.1  thorpej 
     76   1.1  thorpej 	memset(pc, 0, sizeof(*pc));
     77   1.1  thorpej 
     78   1.1  thorpej 	pc->pc_attach_hook = gaps_attach_hook;
     79   1.1  thorpej 	pc->pc_bus_maxdevs = gaps_bus_maxdevs;
     80   1.1  thorpej 	pc->pc_make_tag = gaps_make_tag;
     81   1.4  thorpej 	pc->pc_decompose_tag = gaps_decompose_tag;
     82   1.1  thorpej 	pc->pc_conf_read = gaps_conf_read;
     83   1.1  thorpej 	pc->pc_conf_write = gaps_conf_write;
     84   1.1  thorpej 	pc->pc_conf_v = sc;
     85   1.1  thorpej 
     86   1.1  thorpej 	pc->pc_intr_map = gaps_intr_map;
     87   1.1  thorpej 	pc->pc_intr_string = gaps_intr_string;
     88   1.1  thorpej 	pc->pc_intr_establish = gaps_intr_establish;
     89   1.1  thorpej 	pc->pc_intr_disestablish = gaps_intr_disestablish;
     90   1.1  thorpej 
     91   1.1  thorpej 	if (bus_space_map(sc->sc_memt, 0x01001600, 0x100,
     92   1.1  thorpej 	    0, &sc->sc_pci_memh) != 0)
     93   1.1  thorpej 		panic("gaps_pci_init: can't map PCI configuration space");
     94   1.1  thorpej }
     95   1.1  thorpej 
     96   1.1  thorpej #define	GAPS_PCITAG_MAGIC	0x022473
     97   1.1  thorpej 
     98   1.1  thorpej void
     99   1.1  thorpej gaps_attach_hook(struct device *bus, struct device *pci,
    100   1.1  thorpej     struct pcibus_attach_args *pba)
    101   1.1  thorpej {
    102   1.9  tsutsui 	struct gaps_softc *sc = (void *)bus;
    103   1.1  thorpej 
    104   1.1  thorpej 	/*
    105   1.1  thorpej 	 * Now that we know there's a bus configured, go ahead and
    106   1.1  thorpej 	 * program the BAR on the device.
    107   1.1  thorpej 	 */
    108   1.1  thorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC,
    109   1.1  thorpej 	    PCI_MAPREG_START + 4, 0x01000000);
    110   1.1  thorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC, PCI_COMMAND_STATUS_REG,
    111   1.1  thorpej 	    pci_conf_read(&sc->sc_pc, 0, PCI_COMMAND_STATUS_REG) |
    112   1.1  thorpej 	    PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    113   1.1  thorpej }
    114   1.1  thorpej 
    115   1.1  thorpej int
    116   1.1  thorpej gaps_bus_maxdevs(void *v, int bus)
    117   1.1  thorpej {
    118   1.1  thorpej 
    119   1.7  tsutsui 	return 1;
    120   1.1  thorpej }
    121   1.1  thorpej 
    122   1.1  thorpej pcitag_t
    123   1.1  thorpej gaps_make_tag(void *v, int bus, int dev, int func)
    124   1.1  thorpej {
    125   1.1  thorpej 
    126   1.1  thorpej 	if (bus == 0 && dev == 0 && func == 0)
    127   1.7  tsutsui 		return GAPS_PCITAG_MAGIC;
    128   1.1  thorpej 
    129   1.7  tsutsui 	return 0;
    130   1.4  thorpej }
    131   1.4  thorpej 
    132   1.4  thorpej void
    133   1.4  thorpej gaps_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    134   1.4  thorpej {
    135   1.4  thorpej 	int b, d, f;
    136   1.4  thorpej 
    137   1.4  thorpej 	if (tag == GAPS_PCITAG_MAGIC)
    138   1.4  thorpej 		b = d = f = 0;
    139   1.4  thorpej 	else {
    140   1.4  thorpej 		/*
    141   1.4  thorpej 		 * Invalid for GAPS.  These values ensure that a valid
    142   1.4  thorpej 		 * tag cannot be built.
    143   1.4  thorpej 		 */
    144   1.4  thorpej 		b = 0xff;
    145   1.4  thorpej 		d = 0x1f;
    146   1.4  thorpej 		f = 0x7;
    147   1.4  thorpej 	}
    148   1.4  thorpej 
    149   1.4  thorpej 	if (bp != NULL)
    150   1.4  thorpej 		*bp = b;
    151   1.4  thorpej 	if (dp != NULL)
    152   1.4  thorpej 		*dp = d;
    153   1.4  thorpej 	if (fp != NULL)
    154   1.4  thorpej 		*fp = f;
    155   1.1  thorpej }
    156   1.1  thorpej 
    157   1.1  thorpej pcireg_t
    158   1.1  thorpej gaps_conf_read(void *v, pcitag_t tag, int reg)
    159   1.1  thorpej {
    160   1.1  thorpej 	struct gaps_softc *sc = v;
    161   1.1  thorpej 
    162   1.1  thorpej 	if (tag != GAPS_PCITAG_MAGIC)
    163   1.7  tsutsui 		return -1;
    164   1.1  thorpej 
    165   1.1  thorpej 	if (reg == (PCI_MAPREG_START + 4)) {
    166   1.1  thorpej 		/*
    167   1.1  thorpej 		 * We fake the BAR -- just return the physical address
    168   1.1  thorpej 		 * to which the device is mapped.
    169   1.1  thorpej 		 */
    170   1.7  tsutsui 		return 0x01001700;
    171   1.1  thorpej 	}
    172   1.1  thorpej 
    173   1.7  tsutsui 	return bus_space_read_4(sc->sc_memt, sc->sc_pci_memh, reg);
    174   1.1  thorpej }
    175   1.1  thorpej 
    176   1.1  thorpej void
    177   1.1  thorpej gaps_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
    178   1.1  thorpej {
    179   1.1  thorpej 	struct gaps_softc *sc = v;
    180   1.1  thorpej 
    181   1.1  thorpej 	if (tag != GAPS_PCITAG_MAGIC)
    182   1.1  thorpej 		return;
    183   1.1  thorpej 
    184   1.1  thorpej 	/* Disallow writing to the "BAR" ... it doesn't actually exist. */
    185   1.1  thorpej 	if (reg == (PCI_MAPREG_START + 4) && val != 0x01000000)
    186   1.1  thorpej 		return;
    187   1.1  thorpej 
    188   1.1  thorpej 	bus_space_write_4(sc->sc_memt, sc->sc_pci_memh, reg, val);
    189   1.1  thorpej }
    190   1.1  thorpej 
    191   1.1  thorpej int
    192   1.1  thorpej gaps_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    193   1.1  thorpej {
    194   1.1  thorpej 
    195   1.3      uch 	*ihp = SYSASIC_EVENT_EXT;
    196   1.7  tsutsui 	return 0;
    197   1.1  thorpej }
    198   1.1  thorpej 
    199   1.1  thorpej const char *
    200   1.1  thorpej gaps_intr_string(void *v, pci_intr_handle_t ih)
    201   1.1  thorpej {
    202   1.1  thorpej 
    203   1.5    itohy 	return sysasic_intr_string(IPL_NET);
    204   1.1  thorpej }
    205   1.1  thorpej 
    206   1.1  thorpej void *
    207   1.1  thorpej gaps_intr_establish(void *v, pci_intr_handle_t ih, int level,
    208   1.1  thorpej     int (*func)(void *), void *arg)
    209   1.1  thorpej {
    210   1.3      uch 
    211  1.10  tsutsui 	return sysasic_intr_establish(ih, level, SYSASIC_IRL11, func, arg);
    212   1.1  thorpej }
    213   1.1  thorpej 
    214   1.1  thorpej void
    215   1.1  thorpej gaps_intr_disestablish(void *v, void *ih)
    216   1.1  thorpej {
    217   1.1  thorpej 
    218   1.7  tsutsui 	return sysasic_intr_disestablish(ih);
    219   1.1  thorpej }
    220