Home | History | Annotate | Line # | Download | only in g2
      1 /*	$NetBSD: gapspci.c,v 1.23 2025/11/24 08:17:29 nia Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Marcus Comstedt
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  *    contributors may be used to endorse or promote products derived
     17  *    from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 __KERNEL_RCSID(0, "$NetBSD: gapspci.c,v 1.23 2025/11/24 08:17:29 nia Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/conf.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/bus.h>
     42 
     43 #include <machine/cpu.h>
     44 
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pcireg.h>
     47 
     48 #include <dev/pci/pcidevs.h>
     49 
     50 #include <dreamcast/dev/g2/g2busvar.h>
     51 #include <dreamcast/dev/g2/gapspcivar.h>
     52 
     53 int	gaps_match(device_t, cfdata_t, void *);
     54 void	gaps_attach(device_t, device_t, void *);
     55 
     56 CFATTACH_DECL_NEW(gapspci, sizeof(struct gaps_softc),
     57     gaps_match, gaps_attach, NULL, NULL);
     58 
     59 int
     60 gaps_match(device_t parent, cfdata_t cf, void *aux)
     61 {
     62 	struct g2bus_attach_args *ga = aux;
     63 	uint8_t idbuf[16];
     64 	bus_space_handle_t tmp_memh;
     65 
     66 	if (bus_space_map(ga->ga_memt, 0x01001400, 0x100, 0, &tmp_memh) != 0)
     67 		return 0;
     68 
     69 	bus_space_read_region_1(ga->ga_memt, tmp_memh, 0, idbuf, sizeof(idbuf));
     70 
     71 	bus_space_unmap(ga->ga_memt, tmp_memh, 0x100);
     72 
     73 	if (strncmp((const char *)idbuf, "GAPSPCI_BRIDGE_2", 16))
     74 		return 0;
     75 
     76 	return 1;
     77 }
     78 
     79 void
     80 gaps_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct g2bus_attach_args *ga = aux;
     83 	struct gaps_softc *sc = device_private(self);
     84 	struct pcibus_attach_args pba;
     85 	int i;
     86 
     87 	printf(": SEGA GAPS PCI Bridge\n");
     88 
     89 	sc->sc_dev = self;
     90 	sc->sc_memt = ga->ga_memt;
     91 
     92 	sc->sc_dmabase = 0x1840000;
     93 	sc->sc_dmasize = 32768;
     94 
     95 	if (bus_space_map(sc->sc_memt, 0x01001400, 0x100,
     96 	    0, &sc->sc_gaps_memh) != 0)
     97 		panic("gaps_attach: can't map GAPS register space");
     98 
     99 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x18, 0x5a14a501);
    100 
    101 	for (i = 0; i < 1000000; i++)
    102 		;
    103 
    104 	if (bus_space_read_4(sc->sc_memt, sc->sc_gaps_memh, 0x18) != 1)
    105 		panic("gaps_attach: GAPS PCI bridge not responding");
    106 
    107 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x20, 0x1000000);
    108 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x24, 0x1000000);
    109 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x28, sc->sc_dmabase);
    110 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x2c,
    111 	    sc->sc_dmabase + sc->sc_dmasize);
    112 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x14, 1);
    113 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x34, 1);
    114 
    115 	gaps_pci_init(sc);
    116 	gaps_dma_init(sc);
    117 
    118 	memset(&pba, 0, sizeof(pba));
    119 
    120 	pba.pba_memt = sc->sc_memt;
    121 	pba.pba_dmat = &sc->sc_dmat;
    122 	pba.pba_dmat64 = NULL;
    123 	pba.pba_bus = 0;
    124 	pba.pba_bridgetag = NULL;
    125 	pba.pba_flags = PCI_FLAGS_MEM_OKAY;
    126 	pba.pba_pc = &sc->sc_pc;
    127 
    128 	config_found(self, &pba, pcibusprint, CFARGS_NONE);
    129 }
    130