Home | History | Annotate | Line # | Download | only in g2
      1  1.18    andvar /*	$NetBSD: gapspci_pci.c,v 1.18 2021/08/07 19:41:13 andvar 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.18    andvar  * PCI configuration 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.18    andvar __KERNEL_RCSID(0, "$NetBSD: gapspci_pci.c,v 1.18 2021/08/07 19:41:13 andvar 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.13    dyoung #include <sys/bus.h>
     48   1.9   tsutsui 
     49   1.1   thorpej #include <machine/cpu.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.12   tsutsui void		gaps_attach_hook(device_t, device_t,
     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.17  knakahar int		gaps_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
     66  1.14    martin const char	*gaps_intr_string(void *, pci_intr_handle_t,
     67  1.14    martin 		    char *buf, size_t len);
     68   1.1   thorpej void		*gaps_intr_establish(void *, pci_intr_handle_t,
     69   1.1   thorpej 		    int, int (*)(void *), void *);
     70   1.1   thorpej void		gaps_intr_disestablish(void *, void *);
     71   1.1   thorpej 
     72   1.1   thorpej void
     73   1.1   thorpej gaps_pci_init(struct gaps_softc *sc)
     74   1.1   thorpej {
     75   1.1   thorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
     76   1.1   thorpej 
     77   1.1   thorpej 	memset(pc, 0, sizeof(*pc));
     78   1.1   thorpej 
     79   1.1   thorpej 	pc->pc_attach_hook = gaps_attach_hook;
     80   1.1   thorpej 	pc->pc_bus_maxdevs = gaps_bus_maxdevs;
     81   1.1   thorpej 	pc->pc_make_tag = gaps_make_tag;
     82   1.4   thorpej 	pc->pc_decompose_tag = gaps_decompose_tag;
     83   1.1   thorpej 	pc->pc_conf_read = gaps_conf_read;
     84   1.1   thorpej 	pc->pc_conf_write = gaps_conf_write;
     85   1.1   thorpej 	pc->pc_conf_v = sc;
     86   1.1   thorpej 
     87   1.1   thorpej 	pc->pc_intr_map = gaps_intr_map;
     88   1.1   thorpej 	pc->pc_intr_string = gaps_intr_string;
     89   1.1   thorpej 	pc->pc_intr_establish = gaps_intr_establish;
     90   1.1   thorpej 	pc->pc_intr_disestablish = gaps_intr_disestablish;
     91   1.1   thorpej 
     92   1.1   thorpej 	if (bus_space_map(sc->sc_memt, 0x01001600, 0x100,
     93   1.1   thorpej 	    0, &sc->sc_pci_memh) != 0)
     94   1.1   thorpej 		panic("gaps_pci_init: can't map PCI configuration space");
     95   1.1   thorpej }
     96   1.1   thorpej 
     97   1.1   thorpej #define	GAPS_PCITAG_MAGIC	0x022473
     98   1.1   thorpej 
     99   1.1   thorpej void
    100  1.12   tsutsui gaps_attach_hook(device_t parent, device_t pci, struct pcibus_attach_args *pba)
    101   1.1   thorpej {
    102  1.12   tsutsui 	struct gaps_softc *sc = device_private(parent);
    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.16   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    166  1.16   msaitoh 		return -1;
    167  1.16   msaitoh 
    168   1.1   thorpej 	if (reg == (PCI_MAPREG_START + 4)) {
    169   1.1   thorpej 		/*
    170   1.1   thorpej 		 * We fake the BAR -- just return the physical address
    171   1.1   thorpej 		 * to which the device is mapped.
    172   1.1   thorpej 		 */
    173   1.7   tsutsui 		return 0x01001700;
    174   1.1   thorpej 	}
    175   1.1   thorpej 
    176   1.7   tsutsui 	return bus_space_read_4(sc->sc_memt, sc->sc_pci_memh, reg);
    177   1.1   thorpej }
    178   1.1   thorpej 
    179   1.1   thorpej void
    180   1.1   thorpej gaps_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
    181   1.1   thorpej {
    182   1.1   thorpej 	struct gaps_softc *sc = v;
    183   1.1   thorpej 
    184   1.1   thorpej 	if (tag != GAPS_PCITAG_MAGIC)
    185   1.1   thorpej 		return;
    186   1.1   thorpej 
    187  1.16   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    188  1.16   msaitoh 		return;
    189  1.16   msaitoh 
    190   1.1   thorpej 	/* Disallow writing to the "BAR" ... it doesn't actually exist. */
    191   1.1   thorpej 	if (reg == (PCI_MAPREG_START + 4) && val != 0x01000000)
    192   1.1   thorpej 		return;
    193   1.1   thorpej 
    194   1.1   thorpej 	bus_space_write_4(sc->sc_memt, sc->sc_pci_memh, reg, val);
    195   1.1   thorpej }
    196   1.1   thorpej 
    197   1.1   thorpej int
    198  1.17  knakahar gaps_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    199   1.1   thorpej {
    200   1.1   thorpej 
    201   1.3       uch 	*ihp = SYSASIC_EVENT_EXT;
    202   1.7   tsutsui 	return 0;
    203   1.1   thorpej }
    204   1.1   thorpej 
    205   1.1   thorpej const char *
    206  1.14    martin gaps_intr_string(void *v, pci_intr_handle_t ih,
    207  1.14    martin     char *buf, size_t len)
    208   1.1   thorpej {
    209   1.1   thorpej 
    210  1.15  christos 	strlcpy(buf, sysasic_intr_string(SYSASIC_IRL11), len);
    211  1.15  christos 	return buf;
    212   1.1   thorpej }
    213   1.1   thorpej 
    214   1.1   thorpej void *
    215   1.1   thorpej gaps_intr_establish(void *v, pci_intr_handle_t ih, int level,
    216   1.1   thorpej     int (*func)(void *), void *arg)
    217   1.1   thorpej {
    218   1.3       uch 
    219  1.10   tsutsui 	return sysasic_intr_establish(ih, level, SYSASIC_IRL11, func, arg);
    220   1.1   thorpej }
    221   1.1   thorpej 
    222   1.1   thorpej void
    223   1.1   thorpej gaps_intr_disestablish(void *v, void *ih)
    224   1.1   thorpej {
    225   1.1   thorpej 
    226   1.7   tsutsui 	return sysasic_intr_disestablish(ih);
    227   1.1   thorpej }
    228