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