xref: /netbsd/sys/arch/dreamcast/dev/g2/gapspci.c (revision c4a72b64)
1 /*	$NetBSD: gapspci.c,v 1.8 2002/10/02 15:45:16 thorpej 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Marcus Comstedt.
18  * 4. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/conf.h>
43 #include <sys/mbuf.h>
44 
45 #include <machine/cpu.h>
46 #include <machine/bus.h>
47 
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 
51 #include <dev/pci/pcidevs.h>
52 
53 #include <dreamcast/dev/g2/g2busvar.h>
54 #include <dreamcast/dev/g2/gapspcivar.h>
55 
56 int	gaps_match(struct device *, struct cfdata *, void *);
57 void	gaps_attach(struct device *, struct device *, void *);
58 
59 int	gaps_print(void *, const char *);
60 
61 CFATTACH_DECL(gapspci, sizeof(struct gaps_softc),
62     gaps_match, gaps_attach, NULL, NULL);
63 
64 int
65 gaps_match(struct device *parent, struct cfdata *match, void *aux)
66 {
67   	struct g2bus_attach_args *ga = aux;
68 	char idbuf[16];
69 	bus_space_handle_t tmp_memh;
70 
71 	if (bus_space_map(ga->ga_memt, 0x01001400, 0x100, 0, &tmp_memh) != 0)
72 		return 0;
73 
74 	bus_space_read_region_1(ga->ga_memt, tmp_memh, 0,
75 				idbuf, sizeof(idbuf));
76 
77 	bus_space_unmap(ga->ga_memt, tmp_memh, 0x100);
78 
79 	if(strncmp(idbuf, "GAPSPCI_BRIDGE_2", 16))
80 		return 0;
81 
82 	return (1);
83 }
84 
85 void
86 gaps_attach(struct device *parent, struct device *self, void *aux)
87 {
88 	struct g2bus_attach_args *ga = aux;
89 	struct gaps_softc *sc = (void *) self;
90 	struct pcibus_attach_args pba;
91 	int i;
92 
93 	printf(": SEGA GAPS PCI Bridge\n");
94 
95 	sc->sc_memt = ga->ga_memt;
96 
97 	sc->sc_dmabase = 0x1840000;
98 	sc->sc_dmasize = 32768;
99 
100 	if (bus_space_map(sc->sc_memt, 0x01001400, 0x100,
101 			  0, &sc->sc_gaps_memh) != 0)
102 		panic("gaps_attach: can't map GAPS register space");
103 
104 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x18, 0x5a14a501);
105 
106 	for(i=0; i<1000000; i++)
107 	  ;
108 
109 	if(bus_space_read_4(sc->sc_memt, sc->sc_gaps_memh, 0x18) != 1)
110 		panic("gaps_attach: GAPS PCI bridge not responding");
111 
112 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x20, 0x1000000);
113 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x24, 0x1000000);
114 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x28, sc->sc_dmabase);
115 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x2c,
116 			  sc->sc_dmabase + sc->sc_dmasize);
117 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x14, 1);
118 	bus_space_write_4(sc->sc_memt, sc->sc_gaps_memh, 0x34, 1);
119 
120 	gaps_pci_init(sc);
121 	gaps_dma_init(sc);
122 
123 	memset(&pba, 0, sizeof(pba));
124 
125 	pba.pba_busname = "pci";
126 	pba.pba_memt = sc->sc_memt;
127 	pba.pba_dmat = &sc->sc_dmat;
128 	pba.pba_bus = 0;
129 	pba.pba_bridgetag = NULL;
130 	pba.pba_flags = PCI_FLAGS_MEM_ENABLED;
131 	pba.pba_pc = &sc->sc_pc;
132 
133 	(void) config_found(self, &pba, gaps_print);
134 }
135 
136 int
137 gaps_print(void *aux, const char *pnp)
138 {
139 	struct pcibus_attach_args *pba = aux;
140 
141 	if (pnp)
142 		printf("%s at %s", pba->pba_busname, pnp);
143 	printf(" bus %d", pba->pba_bus);
144 
145 	return (UNCONF);
146 }
147