gapspci_pci.c revision 1.2 1 1.2 marcus /* $NetBSD: gapspci_pci.c,v 1.2 2001/04/24 19:43:25 marcus 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.1 thorpej * PCI configuraiton 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.1 thorpej
42 1.1 thorpej #include <sys/param.h>
43 1.1 thorpej #include <sys/systm.h>
44 1.1 thorpej #include <sys/kernel.h>
45 1.1 thorpej #include <sys/device.h>
46 1.1 thorpej
47 1.1 thorpej #include <machine/cpu.h>
48 1.1 thorpej #include <machine/bus.h>
49 1.1 thorpej #include <machine/shbvar.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.1 thorpej
57 1.1 thorpej void gaps_attach_hook(struct device *, struct device *,
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.1 thorpej pcireg_t gaps_conf_read(void *, pcitag_t, int);
62 1.1 thorpej void gaps_conf_write(void *, pcitag_t, int, pcireg_t);
63 1.1 thorpej
64 1.1 thorpej int gaps_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
65 1.1 thorpej const char *gaps_intr_string(void *, pci_intr_handle_t);
66 1.1 thorpej void *gaps_intr_establish(void *, pci_intr_handle_t,
67 1.1 thorpej int, int (*)(void *), void *);
68 1.1 thorpej void gaps_intr_disestablish(void *, void *);
69 1.1 thorpej
70 1.1 thorpej void
71 1.1 thorpej gaps_pci_init(struct gaps_softc *sc)
72 1.1 thorpej {
73 1.1 thorpej pci_chipset_tag_t pc = &sc->sc_pc;
74 1.1 thorpej
75 1.1 thorpej memset(pc, 0, sizeof(*pc));
76 1.1 thorpej
77 1.1 thorpej pc->pc_attach_hook = gaps_attach_hook;
78 1.1 thorpej pc->pc_bus_maxdevs = gaps_bus_maxdevs;
79 1.1 thorpej pc->pc_make_tag = gaps_make_tag;
80 1.1 thorpej pc->pc_conf_read = gaps_conf_read;
81 1.1 thorpej pc->pc_conf_write = gaps_conf_write;
82 1.1 thorpej pc->pc_conf_v = sc;
83 1.1 thorpej
84 1.1 thorpej pc->pc_intr_map = gaps_intr_map;
85 1.1 thorpej pc->pc_intr_string = gaps_intr_string;
86 1.1 thorpej pc->pc_intr_establish = gaps_intr_establish;
87 1.1 thorpej pc->pc_intr_disestablish = gaps_intr_disestablish;
88 1.1 thorpej
89 1.1 thorpej if (bus_space_map(sc->sc_memt, 0x01001600, 0x100,
90 1.1 thorpej 0, &sc->sc_pci_memh) != 0)
91 1.1 thorpej panic("gaps_pci_init: can't map PCI configuration space");
92 1.1 thorpej }
93 1.1 thorpej
94 1.1 thorpej #define GAPS_PCITAG_MAGIC 0x022473
95 1.1 thorpej
96 1.1 thorpej void
97 1.1 thorpej gaps_attach_hook(struct device *bus, struct device *pci,
98 1.1 thorpej struct pcibus_attach_args *pba)
99 1.1 thorpej {
100 1.1 thorpej struct gaps_softc *sc = (void *) bus;
101 1.1 thorpej
102 1.1 thorpej /*
103 1.1 thorpej * Now that we know there's a bus configured, go ahead and
104 1.1 thorpej * program the BAR on the device.
105 1.1 thorpej */
106 1.1 thorpej pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC,
107 1.1 thorpej PCI_MAPREG_START + 4, 0x01000000);
108 1.1 thorpej pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC, PCI_COMMAND_STATUS_REG,
109 1.1 thorpej pci_conf_read(&sc->sc_pc, 0, PCI_COMMAND_STATUS_REG) |
110 1.1 thorpej PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
111 1.1 thorpej }
112 1.1 thorpej
113 1.1 thorpej int
114 1.1 thorpej gaps_bus_maxdevs(void *v, int bus)
115 1.1 thorpej {
116 1.1 thorpej
117 1.1 thorpej return (1);
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej pcitag_t
121 1.1 thorpej gaps_make_tag(void *v, int bus, int dev, int func)
122 1.1 thorpej {
123 1.1 thorpej
124 1.1 thorpej if (bus == 0 && dev == 0 && func == 0)
125 1.1 thorpej return (GAPS_PCITAG_MAGIC);
126 1.1 thorpej
127 1.1 thorpej return (0);
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej pcireg_t
131 1.1 thorpej gaps_conf_read(void *v, pcitag_t tag, int reg)
132 1.1 thorpej {
133 1.1 thorpej struct gaps_softc *sc = v;
134 1.1 thorpej
135 1.1 thorpej if (tag != GAPS_PCITAG_MAGIC)
136 1.1 thorpej return (-1);
137 1.1 thorpej
138 1.1 thorpej if (reg == (PCI_MAPREG_START + 4)) {
139 1.1 thorpej /*
140 1.1 thorpej * We fake the BAR -- just return the physical address
141 1.1 thorpej * to which the device is mapped.
142 1.1 thorpej */
143 1.1 thorpej return (0x01001700);
144 1.1 thorpej }
145 1.1 thorpej
146 1.1 thorpej return (bus_space_read_4(sc->sc_memt, sc->sc_pci_memh, reg));
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej void
150 1.1 thorpej gaps_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
151 1.1 thorpej {
152 1.1 thorpej struct gaps_softc *sc = v;
153 1.1 thorpej
154 1.1 thorpej if (tag != GAPS_PCITAG_MAGIC)
155 1.1 thorpej return;
156 1.1 thorpej
157 1.1 thorpej /* Disallow writing to the "BAR" ... it doesn't actually exist. */
158 1.1 thorpej if (reg == (PCI_MAPREG_START + 4) && val != 0x01000000)
159 1.1 thorpej return;
160 1.1 thorpej
161 1.1 thorpej bus_space_write_4(sc->sc_memt, sc->sc_pci_memh, reg, val);
162 1.1 thorpej }
163 1.1 thorpej
164 1.1 thorpej int
165 1.1 thorpej gaps_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
166 1.1 thorpej {
167 1.1 thorpej
168 1.1 thorpej /* We interrupt at the CPU's irq 11. */
169 1.1 thorpej *ihp = 11;
170 1.1 thorpej return (0);
171 1.1 thorpej }
172 1.1 thorpej
173 1.1 thorpej const char *
174 1.1 thorpej gaps_intr_string(void *v, pci_intr_handle_t ih)
175 1.1 thorpej {
176 1.1 thorpej
177 1.1 thorpej return ("SH4 irq 11");
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej void *
181 1.1 thorpej gaps_intr_establish(void *v, pci_intr_handle_t ih, int level,
182 1.1 thorpej int (*func)(void *), void *arg)
183 1.1 thorpej {
184 1.2 marcus return sysasic_intr_establish(ih, SYSASIC_EVENT_EXT,
185 1.2 marcus level, func, arg);
186 1.1 thorpej }
187 1.1 thorpej
188 1.1 thorpej void
189 1.1 thorpej gaps_intr_disestablish(void *v, void *ih)
190 1.1 thorpej {
191 1.1 thorpej
192 1.1 thorpej panic("gaps_intr_disestablish: not implemented");
193 1.1 thorpej }
194