pci_gio.c revision 1.19 1 1.19 thorpej /* $NetBSD: pci_gio.c,v 1.19 2021/08/07 16:19:04 thorpej Exp $ */
2 1.1 rumble
3 1.1 rumble /*
4 1.1 rumble * Copyright (c) 2006 Stephen M. Rumble
5 1.1 rumble * All rights reserved.
6 1.1 rumble *
7 1.1 rumble * Redistribution and use in source and binary forms, with or without
8 1.1 rumble * modification, are permitted provided that the following conditions
9 1.1 rumble * are met:
10 1.1 rumble * 1. Redistributions of source code must retain the above copyright
11 1.1 rumble * notice, this list of conditions and the following disclaimer.
12 1.1 rumble * 2. The name of the author may not be used to endorse or promote products
13 1.1 rumble * derived from this software without specific prior written permission.
14 1.1 rumble *
15 1.1 rumble * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 rumble * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 rumble * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 rumble * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 rumble * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 rumble * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 rumble * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 rumble * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 rumble * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 rumble * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 rumble */
26 1.1 rumble
27 1.1 rumble #include <sys/cdefs.h>
28 1.19 thorpej __KERNEL_RCSID(0, "$NetBSD: pci_gio.c,v 1.19 2021/08/07 16:19:04 thorpej Exp $");
29 1.1 rumble
30 1.1 rumble /*
31 1.1 rumble * Glue for PCI devices that are connected to the GIO bus by various little
32 1.1 rumble * GIO<->PCI ASICs.
33 1.1 rumble *
34 1.1 rumble * We presently support the following boards:
35 1.1 rumble * o Phobos G100/G130/G160 (if_tlp, lxtphy)
36 1.1 rumble * o Set Engineering GFE (if_tl, nsphy)
37 1.1 rumble */
38 1.1 rumble
39 1.1 rumble #include "opt_pci.h"
40 1.1 rumble #include "pci.h"
41 1.1 rumble
42 1.1 rumble #include <sys/param.h>
43 1.1 rumble #include <sys/systm.h>
44 1.1 rumble #include <sys/device.h>
45 1.1 rumble #include <sys/malloc.h>
46 1.1 rumble
47 1.9 dyoung #include <sys/bus.h>
48 1.1 rumble #include <machine/machtype.h>
49 1.1 rumble
50 1.1 rumble #include <sgimips/gio/giovar.h>
51 1.1 rumble #include <sgimips/gio/gioreg.h>
52 1.1 rumble #include <sgimips/gio/giodevs.h>
53 1.1 rumble
54 1.1 rumble #include <sgimips/dev/imcvar.h>
55 1.1 rumble
56 1.1 rumble #include <mips/cache.h>
57 1.1 rumble
58 1.1 rumble #include <dev/pci/pcivar.h>
59 1.1 rumble #include <dev/pci/pcireg.h>
60 1.1 rumble #include <dev/pci/pcidevs.h>
61 1.1 rumble #include <dev/pci/pciconf.h>
62 1.1 rumble
63 1.1 rumble int giopci_debug = 0;
64 1.1 rumble #define DPRINTF(_x) if (giopci_debug) printf _x
65 1.1 rumble
66 1.1 rumble struct giopci_softc {
67 1.1 rumble struct sgimips_pci_chipset sc_pc;
68 1.1 rumble int sc_slot;
69 1.1 rumble int sc_gprid;
70 1.2 rumble uint32_t sc_pci_len;
71 1.1 rumble bus_space_tag_t sc_iot;
72 1.1 rumble bus_space_handle_t sc_ioh;
73 1.1 rumble };
74 1.1 rumble
75 1.11 chs static int giopci_match(device_t, cfdata_t, void *);
76 1.11 chs static void giopci_attach(device_t, device_t, void *);
77 1.1 rumble static int giopci_bus_maxdevs(pci_chipset_tag_t, int);
78 1.1 rumble static pcireg_t giopci_conf_read(pci_chipset_tag_t, pcitag_t, int);
79 1.1 rumble static void giopci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
80 1.1 rumble static int giopci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
81 1.7 dyoung static int giopci_intr_map(const struct pci_attach_args *,
82 1.7 dyoung pci_intr_handle_t *);
83 1.1 rumble static const char *
84 1.12 christos giopci_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
85 1.12 christos char *, size_t);
86 1.1 rumble static void *giopci_intr_establish(int, int, int (*)(void *), void *);
87 1.1 rumble static void giopci_intr_disestablish(void *);
88 1.1 rumble
89 1.1 rumble #define PHOBOS_PCI_OFFSET 0x00100000
90 1.1 rumble #define PHOBOS_PCI_LENGTH 128 /* ~arbitrary */
91 1.1 rumble #define PHOBOS_TULIP_START 0x00101000
92 1.1 rumble #define PHOBOS_TULIP_END 0x001fffff
93 1.1 rumble
94 1.1 rumble #define SETENG_MAGIC_OFFSET 0x00020000
95 1.1 rumble #define SETENG_MAGIC_VALUE 0x00001000
96 1.1 rumble #define SETENG_PCI_OFFSET 0x00080000
97 1.1 rumble #define SETENG_PCI_LENGTH 128 /* ~arbitrary */
98 1.1 rumble #define SETENG_TLAN_START 0x00100000
99 1.1 rumble #define SETENG_TLAN_END 0x001fffff
100 1.1 rumble
101 1.11 chs CFATTACH_DECL_NEW(giopci, sizeof(struct giopci_softc),
102 1.1 rumble giopci_match, giopci_attach, NULL, NULL);
103 1.1 rumble
104 1.14 macallan static void pcimem_bus_mem_init(bus_space_tag_t, void *);
105 1.14 macallan static struct mips_bus_space pcimem_mbst;
106 1.14 macallan bus_space_tag_t gio_pci_memt = NULL;
107 1.14 macallan
108 1.1 rumble static int
109 1.11 chs giopci_match(device_t parent, cfdata_t match, void *aux)
110 1.1 rumble {
111 1.1 rumble struct gio_attach_args *ga = aux;
112 1.1 rumble int gprid;
113 1.1 rumble
114 1.1 rumble /*
115 1.3 rumble * I think that these cards are all GIO32-bis or GIO64. Thus
116 1.3 rumble * they work in either Indigo2/Challenge M or
117 1.3 rumble * Indy/Challenge S/Indigo R4k, according to form factor. However,
118 1.1 rumble * there are some exceptions (e.g. my Indigo R4k won't power
119 1.1 rumble * on with the Set Engineering card installed).
120 1.1 rumble */
121 1.1 rumble if (mach_type != MACH_SGI_IP20 && mach_type != MACH_SGI_IP22)
122 1.1 rumble return (0);
123 1.1 rumble
124 1.1 rumble gprid = GIO_PRODUCT_PRODUCTID(ga->ga_product);
125 1.1 rumble if (gprid == PHOBOS_G100 || gprid == PHOBOS_G130 ||
126 1.1 rumble gprid == PHOBOS_G160 || gprid == SETENG_GFE)
127 1.1 rumble return (1);
128 1.1 rumble
129 1.1 rumble return (0);
130 1.1 rumble }
131 1.1 rumble
132 1.1 rumble static void
133 1.11 chs giopci_attach(device_t parent, device_t self, void *aux)
134 1.1 rumble {
135 1.11 chs struct giopci_softc *sc = device_private(self);
136 1.1 rumble pci_chipset_tag_t pc = &sc->sc_pc;
137 1.1 rumble struct gio_attach_args *ga = aux;
138 1.1 rumble uint32_t pci_off, pci_len, arb;
139 1.1 rumble struct pcibus_attach_args pba;
140 1.1 rumble u_long m_start, m_end;
141 1.1 rumble #ifdef PCI_NETBSD_CONFIGURE
142 1.1 rumble extern int pci_conf_debug;
143 1.1 rumble
144 1.1 rumble pci_conf_debug = giopci_debug;
145 1.1 rumble #endif
146 1.1 rumble
147 1.1 rumble sc->sc_iot = ga->ga_iot;
148 1.1 rumble sc->sc_slot = ga->ga_slot;
149 1.1 rumble sc->sc_gprid = GIO_PRODUCT_PRODUCTID(ga->ga_product);
150 1.1 rumble
151 1.14 macallan pcimem_bus_mem_init(&pcimem_mbst, NULL);
152 1.14 macallan gio_pci_memt = &pcimem_mbst;
153 1.14 macallan
154 1.1 rumble if (mach_type == MACH_SGI_IP22 &&
155 1.1 rumble mach_subtype == MACH_SGI_IP22_FULLHOUSE)
156 1.1 rumble arb = GIO_ARB_RT | GIO_ARB_MST | GIO_ARB_PIPE;
157 1.1 rumble else
158 1.1 rumble arb = GIO_ARB_RT | GIO_ARB_MST;
159 1.1 rumble
160 1.1 rumble if (gio_arb_config(ga->ga_slot, arb)) {
161 1.1 rumble printf(": failed to configure GIO bus arbiter\n");
162 1.1 rumble return;
163 1.1 rumble }
164 1.1 rumble
165 1.1 rumble #if (NIMC > 0)
166 1.1 rumble imc_disable_sysad_parity();
167 1.1 rumble #endif
168 1.1 rumble
169 1.1 rumble switch (sc->sc_gprid) {
170 1.1 rumble case PHOBOS_G100:
171 1.1 rumble case PHOBOS_G130:
172 1.1 rumble case PHOBOS_G160:
173 1.1 rumble pci_off = PHOBOS_PCI_OFFSET;
174 1.1 rumble pci_len = PHOBOS_PCI_LENGTH;
175 1.1 rumble m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_START);
176 1.1 rumble m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_END);
177 1.1 rumble break;
178 1.1 rumble
179 1.1 rumble case SETENG_GFE:
180 1.4 rumble /*
181 1.4 rumble * NB: The SetEng board does not allow the ThunderLAN's DMA
182 1.4 rumble * engine to properly transfer segments that span page
183 1.4 rumble * boundaries. See sgimips/autoconf.c where we catch a
184 1.4 rumble * tl(4) device attachment and create an appropriate
185 1.4 rumble * proplib entry to enable the workaround.
186 1.4 rumble */
187 1.1 rumble pci_off = SETENG_PCI_OFFSET;
188 1.1 rumble pci_len = SETENG_PCI_LENGTH;
189 1.1 rumble m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_START);
190 1.1 rumble m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_END);
191 1.1 rumble bus_space_write_4(ga->ga_iot, ga->ga_ioh,
192 1.1 rumble SETENG_MAGIC_OFFSET, SETENG_MAGIC_VALUE);
193 1.1 rumble break;
194 1.1 rumble
195 1.1 rumble default:
196 1.1 rumble panic("giopci_attach: unsupported GIO product id 0x%02x",
197 1.1 rumble sc->sc_gprid);
198 1.1 rumble }
199 1.1 rumble
200 1.1 rumble if (bus_space_subregion(ga->ga_iot, ga->ga_ioh, pci_off, pci_len,
201 1.1 rumble &sc->sc_ioh)) {
202 1.11 chs printf("%s: unable to map PCI registers\n", device_xname(self));
203 1.1 rumble return;
204 1.1 rumble }
205 1.2 rumble sc->sc_pci_len = pci_len;
206 1.1 rumble
207 1.1 rumble pc->pc_bus_maxdevs = giopci_bus_maxdevs;
208 1.1 rumble pc->pc_conf_read = giopci_conf_read;
209 1.1 rumble pc->pc_conf_write = giopci_conf_write;
210 1.1 rumble pc->pc_conf_hook = giopci_conf_hook;
211 1.1 rumble pc->pc_intr_map = giopci_intr_map;
212 1.1 rumble pc->pc_intr_string = giopci_intr_string;
213 1.1 rumble pc->intr_establish = giopci_intr_establish;
214 1.1 rumble pc->intr_disestablish = giopci_intr_disestablish;
215 1.1 rumble pc->iot = ga->ga_iot;
216 1.1 rumble pc->ioh = ga->ga_ioh;
217 1.1 rumble pc->cookie = sc;
218 1.1 rumble
219 1.1 rumble printf(": %s\n", gio_product_string(sc->sc_gprid));
220 1.1 rumble
221 1.1 rumble #ifdef PCI_NETBSD_CONFIGURE
222 1.17 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
223 1.17 thorpej
224 1.17 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
225 1.17 thorpej m_start, (m_end - m_start) + 1);
226 1.17 thorpej
227 1.17 thorpej pci_configure_bus(pc, pcires, 0,
228 1.6 matt mips_cache_info.mci_dcache_align);
229 1.17 thorpej
230 1.17 thorpej pciconf_resource_fini(pcires);
231 1.1 rumble #endif
232 1.1 rumble
233 1.1 rumble memset(&pba, 0, sizeof(pba));
234 1.14 macallan pba.pba_memt = gio_pci_memt;
235 1.1 rumble pba.pba_dmat = ga->ga_dmat;
236 1.1 rumble pba.pba_pc = pc;
237 1.8 dyoung pba.pba_flags = PCI_FLAGS_MEM_OKAY;
238 1.1 rumble /* NB: do not set PCI_FLAGS_{MRL,MRM,MWI}_OKAY -- true ?! */
239 1.1 rumble
240 1.19 thorpej config_found(self, &pba, pcibusprint, CFARGS_NONE);
241 1.1 rumble }
242 1.1 rumble
243 1.1 rumble static int
244 1.1 rumble giopci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
245 1.1 rumble {
246 1.1 rumble
247 1.1 rumble return (busno == 0);
248 1.1 rumble }
249 1.1 rumble
250 1.1 rumble static pcireg_t
251 1.1 rumble giopci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
252 1.1 rumble {
253 1.1 rumble struct giopci_softc *sc = pc->cookie;
254 1.1 rumble int bus, dev, func;
255 1.1 rumble pcireg_t data;
256 1.1 rumble
257 1.15 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
258 1.15 msaitoh return (pcireg_t) -1;
259 1.15 msaitoh
260 1.1 rumble pci_decompose_tag(pc, tag, &bus, &dev, &func);
261 1.1 rumble if (bus != 0 || dev != 0 || func != 0)
262 1.1 rumble return (0);
263 1.1 rumble
264 1.2 rumble /* XXX - should just use bus_space_peek */
265 1.2 rumble if (reg >= sc->sc_pci_len) {
266 1.2 rumble DPRINTF(("giopci_conf_read: reg 0x%x out of bounds\n", reg));
267 1.2 rumble return (0);
268 1.2 rumble }
269 1.2 rumble
270 1.1 rumble DPRINTF(("giopci_conf_read: reg 0x%x = 0x", reg));
271 1.1 rumble data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
272 1.1 rumble DPRINTF(("%08x\n", data));
273 1.1 rumble
274 1.1 rumble return (data);
275 1.1 rumble }
276 1.1 rumble
277 1.1 rumble static void
278 1.1 rumble giopci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
279 1.1 rumble {
280 1.1 rumble struct giopci_softc *sc = pc->cookie;
281 1.1 rumble int bus, dev, func;
282 1.1 rumble
283 1.15 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
284 1.15 msaitoh return;
285 1.15 msaitoh
286 1.1 rumble pci_decompose_tag(pc, tag, &bus, &dev, &func);
287 1.1 rumble if (bus != 0 || dev != 0 || func != 0)
288 1.1 rumble return;
289 1.1 rumble
290 1.2 rumble /* XXX - should just use bus_space_poke */
291 1.2 rumble if (reg >= sc->sc_pci_len) {
292 1.2 rumble DPRINTF(("giopci_conf_write: reg 0x%x out of bounds "
293 1.2 rumble "(val = 0x%08x)\n", reg, data));
294 1.2 rumble return;
295 1.2 rumble }
296 1.2 rumble
297 1.1 rumble DPRINTF(("giopci_conf_write: reg 0x%x = 0x%08x\n", reg, data));
298 1.1 rumble bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, data);
299 1.1 rumble }
300 1.1 rumble
301 1.1 rumble static int
302 1.1 rumble giopci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function,
303 1.1 rumble pcireg_t id)
304 1.1 rumble {
305 1.1 rumble
306 1.1 rumble /* All devices use memory accesses only. */
307 1.1 rumble return (PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_BM);
308 1.1 rumble }
309 1.1 rumble
310 1.1 rumble static int
311 1.7 dyoung giopci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
312 1.1 rumble {
313 1.1 rumble struct giopci_softc *sc = pa->pa_pc->cookie;
314 1.1 rumble
315 1.1 rumble *ihp = sc->sc_slot;
316 1.1 rumble
317 1.1 rumble return (0);
318 1.1 rumble }
319 1.1 rumble
320 1.1 rumble static const char *
321 1.12 christos giopci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char * buf,
322 1.13 ozaki size_t len)
323 1.1 rumble {
324 1.12 christos snprintf(buf, len, "slot %s", (ih == GIO_SLOT_EXP0) ? "EXP0" :
325 1.1 rumble (ih == GIO_SLOT_EXP1) ? "EXP1" : "GFX");
326 1.12 christos return buf;
327 1.1 rumble }
328 1.1 rumble
329 1.1 rumble static void *
330 1.1 rumble giopci_intr_establish(int slot, int level, int (*func)(void *), void *arg)
331 1.1 rumble {
332 1.1 rumble
333 1.1 rumble return (gio_intr_establish(slot, level, func, arg));
334 1.1 rumble }
335 1.1 rumble
336 1.1 rumble static void
337 1.1 rumble giopci_intr_disestablish(void *cookie)
338 1.1 rumble {
339 1.1 rumble
340 1.1 rumble panic("giopci_intr_disestablish: impossible.");
341 1.1 rumble }
342 1.14 macallan
343 1.14 macallan #define CHIP pcimem
344 1.14 macallan #define CHIP_MEM /* defined */
345 1.14 macallan #define CHIP_WRONG_ENDIAN
346 1.14 macallan
347 1.14 macallan #define CHIP_W1_BUS_START(v) 0x00000000UL
348 1.14 macallan #define CHIP_W1_BUS_END(v) 0xffffffffUL
349 1.14 macallan #define CHIP_W1_SYS_START(v) 0x00000000UL
350 1.14 macallan #define CHIP_W1_SYS_END(v) 0xffffffffUL
351 1.14 macallan
352 1.14 macallan #include <mips/mips/bus_space_alignstride_chipdep.c>
353