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