cia.c revision 1.79 1 /* $NetBSD: cia.c,v 1.79 2021/08/07 16:18:41 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
35 * All rights reserved.
36 *
37 * Author: Chris G. Demetriou
38 *
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
44 *
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * Carnegie Mellon requests users of this software to return to
50 *
51 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
55 *
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
58 */
59
60 #include "opt_dec_550.h"
61
62 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
63
64 __KERNEL_RCSID(0, "$NetBSD: cia.c,v 1.79 2021/08/07 16:18:41 thorpej Exp $");
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/device.h>
70
71 #include <machine/autoconf.h>
72 #include <machine/rpb.h>
73 #include <machine/sysarch.h>
74 #include <machine/alpha.h>
75
76 #include <dev/isa/isareg.h>
77 #include <dev/isa/isavar.h>
78
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
81 #include <alpha/pci/ciareg.h>
82 #include <alpha/pci/ciavar.h>
83
84 static int ciamatch(device_t, cfdata_t, void *);
85 static void ciaattach(device_t, device_t, void *);
86
87 CFATTACH_DECL_NEW(cia, sizeof(struct cia_softc),
88 ciamatch, ciaattach, NULL, NULL);
89
90 extern struct cfdriver cia_cd;
91
92 static int cia_bus_get_window(int, int,
93 struct alpha_bus_space_translation *);
94
95 /* There can be only one. */
96 static int ciafound;
97 struct cia_config cia_configuration;
98
99 /*
100 * This determines if we attempt to use BWX for PCI bus and config space
101 * access. Some systems, notably with Pyxis, don't fare so well unless
102 * BWX is used.
103 *
104 * EXCEPT! Some devices have a really hard time if BWX is used (WHY?!).
105 * So, we decouple the uses for PCI config space and PCI bus space.
106 *
107 * FURTHERMORE! The Pyxis, most notably earlier revs, really don't
108 * do so well if you don't use BWX for bus access. So we default to
109 * forcing BWX on those chips.
110 *
111 * Geez.
112 */
113
114 #ifndef CIA_PCI_USE_BWX
115 #define CIA_PCI_USE_BWX 1
116 #endif
117
118 #ifndef CIA_BUS_USE_BWX
119 #define CIA_BUS_USE_BWX 0
120 #endif
121
122 #ifndef CIA_PYXIS_FORCE_BWX
123 #define CIA_PYXIS_FORCE_BWX 0
124 #endif
125
126 int cia_pci_use_bwx = CIA_PCI_USE_BWX;
127 int cia_bus_use_bwx = CIA_BUS_USE_BWX;
128 int cia_pyxis_force_bwx = CIA_PYXIS_FORCE_BWX;
129
130 static int
131 ciamatch(device_t parent, cfdata_t match, void *aux)
132 {
133 struct mainbus_attach_args *ma = aux;
134
135 /* Make sure that we're looking for a CIA. */
136 if (strcmp(ma->ma_name, cia_cd.cd_name) != 0)
137 return (0);
138
139 if (ciafound)
140 return (0);
141
142 return (1);
143 }
144
145 /*
146 * Set up the chipset's function pointers.
147 */
148 void
149 cia_init(struct cia_config *ccp, int mallocsafe)
150 {
151 int pci_use_bwx = cia_pci_use_bwx;
152 int bus_use_bwx = cia_bus_use_bwx;
153
154 ccp->cc_hae_mem = REGVAL(CIA_CSR_HAE_MEM);
155 ccp->cc_hae_io = REGVAL(CIA_CSR_HAE_IO);
156 ccp->cc_rev = REGVAL(CIA_CSR_REV) & REV_MASK;
157
158 /*
159 * Determine if we have a Pyxis. Only two systypes can
160 * have this: the EB164 systype (AlphaPC164LX and AlphaPC164SX)
161 * and the DEC_550 systype (Miata).
162 */
163 if ((cputype == ST_EB164 &&
164 (hwrpb->rpb_variation & SV_ST_MASK) >= SV_ST_ALPHAPC164LX_400) ||
165 cputype == ST_DEC_550) {
166 ccp->cc_flags |= CCF_ISPYXIS;
167 if (cia_pyxis_force_bwx)
168 pci_use_bwx = bus_use_bwx = 1;
169 }
170
171 /*
172 * ALCOR/ALCOR2 Revisions >= 2 and Pyxis have the CNFG register.
173 */
174 if (ccp->cc_rev >= 2 || (ccp->cc_flags & CCF_ISPYXIS) != 0)
175 ccp->cc_cnfg = REGVAL(CIA_CSR_CNFG);
176 else
177 ccp->cc_cnfg = 0;
178
179 /*
180 * Use BWX iff:
181 *
182 * - It hasn't been disbled by the user,
183 * - it's enabled in CNFG,
184 * - we're implementation version ev5,
185 * - BWX is enabled in the CPU's capabilities mask
186 */
187 if ((pci_use_bwx || bus_use_bwx) &&
188 (ccp->cc_cnfg & CNFG_BWEN) != 0 &&
189 (cpu_amask & ALPHA_AMASK_BWX) != 0) {
190 uint32_t ctrl;
191
192 if (pci_use_bwx)
193 ccp->cc_flags |= CCF_PCI_USE_BWX;
194 if (bus_use_bwx)
195 ccp->cc_flags |= CCF_BUS_USE_BWX;
196
197 /*
198 * For whatever reason, the firmware seems to enable PCI
199 * loopback mode if it also enables BWX. Make sure it's
200 * enabled if we have an old, buggy firmware rev.
201 */
202 alpha_mb();
203 ctrl = REGVAL(CIA_CSR_CTRL);
204 if ((ctrl & CTRL_PCI_LOOP_EN) == 0) {
205 REGVAL(CIA_CSR_CTRL) = ctrl | CTRL_PCI_LOOP_EN;
206 alpha_mb();
207 }
208 }
209
210 if (!ccp->cc_initted) {
211 /* don't do these twice since they set up extents */
212 if (ccp->cc_flags & CCF_BUS_USE_BWX) {
213 cia_bwx_bus_io_init(&ccp->cc_iot, ccp);
214 cia_bwx_bus_mem_init(&ccp->cc_memt, ccp);
215
216 /*
217 * We have one window for both PCI I/O and MEM
218 * in BWX mode.
219 */
220 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 1;
221 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 1;
222 } else {
223 cia_swiz_bus_io_init(&ccp->cc_iot, ccp);
224 cia_swiz_bus_mem_init(&ccp->cc_memt, ccp);
225
226 /*
227 * We have two I/O windows and 4 MEM windows in
228 * SWIZ mode.
229 */
230 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 2;
231 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 4;
232 }
233 alpha_bus_get_window = cia_bus_get_window;
234 }
235 ccp->cc_mallocsafe = mallocsafe;
236
237 cia_pci_init(&ccp->cc_pc, ccp);
238 alpha_pci_chipset = &ccp->cc_pc;
239
240 ccp->cc_initted = 1;
241 }
242
243 static void
244 ciaattach(device_t parent, device_t self, void *aux)
245 {
246 struct cia_softc *sc = device_private(self);
247 struct cia_config *ccp;
248 struct pcibus_attach_args pba;
249 char bits[64];
250 const char *name;
251
252 /* note that we've attached the chipset; can't have 2 CIAs. */
253 ciafound = 1;
254 sc->sc_dev = self;
255
256 /*
257 * set up the chipset's info; done once at console init time
258 * (maybe), but we must do it here as well to take care of things
259 * that need to use memory allocation.
260 */
261 ccp = sc->sc_ccp = &cia_configuration;
262 cia_init(ccp, 1);
263
264 if (ccp->cc_flags & CCF_ISPYXIS) {
265 name = "Pyxis";
266 } else {
267 name = "ALCOR/ALCOR2";
268 }
269
270 aprint_normal(": DECchip 2117x Core Logic Chipset (%s), pass %d\n",
271 name, ccp->cc_rev + 1);
272 if (ccp->cc_cnfg) {
273 snprintb(bits, sizeof(bits), CIA_CSR_CNFG_BITS, ccp->cc_cnfg);
274 aprint_normal_dev(self, "extended capabilities: %s\n", bits);
275 }
276
277 switch (ccp->cc_flags & (CCF_PCI_USE_BWX|CCF_BUS_USE_BWX)) {
278 case CCF_PCI_USE_BWX|CCF_BUS_USE_BWX:
279 name = "PCI config and bus";
280 break;
281 case CCF_PCI_USE_BWX:
282 name = "PCI config";
283 break;
284 case CCF_BUS_USE_BWX:
285 name = "bus";
286 break;
287 default:
288 name = NULL;
289 break;
290 }
291 if (name != NULL)
292 aprint_normal_dev(self, "using BWX for %s access\n", name);
293
294 #ifdef DEC_550
295 if (cputype == ST_DEC_550 &&
296 (hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
297 /*
298 * Miata 1 systems have a bug: DMA cannot cross
299 * an 8k boundary! Make sure PCI read prefetching
300 * is disabled on these chips. Note that secondary
301 * PCI busses don't have this problem, because of
302 * the way PPBs handle PCI read requests.
303 *
304 * In the 21174 Technical Reference Manual, this is
305 * actually documented as "Pyxis Pass 1", but apparently
306 * there are chips that report themselves as "Pass 1"
307 * which do not have the bug! Miatas with the Cypress
308 * PCI-ISA bridge (i.e. Miata 1.5 and Miata 2) do not
309 * have the bug, so we use this check.
310 *
311 * NOTE: This bug is actually worked around in cia_dma.c,
312 * when direct-mapped DMA maps are created.
313 *
314 * XXX WE NEED TO THINK ABOUT HOW TO HANDLE THIS FOR
315 * XXX SGMAP DMA MAPPINGS!
316 */
317 uint32_t ctrl;
318
319 /* XXX no bets... */
320 aprint_error_dev(self,
321 "WARNING: Pyxis pass 1 DMA bug; no bets...\n");
322
323 ccp->cc_flags |= CCF_PYXISBUG;
324
325 alpha_mb();
326 ctrl = REGVAL(CIA_CSR_CTRL);
327 ctrl &= ~(CTRL_RD_TYPE|CTRL_RL_TYPE|CTRL_RM_TYPE);
328 REGVAL(CIA_CSR_CTRL) = ctrl;
329 alpha_mb();
330 }
331 #endif /* DEC_550 */
332
333 cia_dma_init(ccp);
334
335 alpha_pci_intr_init(ccp, &ccp->cc_iot, &ccp->cc_memt, &ccp->cc_pc);
336
337 pba.pba_iot = &ccp->cc_iot;
338 pba.pba_memt = &ccp->cc_memt;
339 pba.pba_dmat =
340 alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI);
341 pba.pba_dmat64 = NULL;
342 pba.pba_pc = &ccp->cc_pc;
343 pba.pba_bus = 0;
344 pba.pba_bridgetag = NULL;
345 pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
346 if ((ccp->cc_flags & CCF_PYXISBUG) == 0)
347 pba.pba_flags |= PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
348 PCI_FLAGS_MWI_OKAY;
349 config_found(self, &pba, pcibusprint, CFARGS_NONE);
350 }
351
352 static int
353 cia_bus_get_window(int type, int window,
354 struct alpha_bus_space_translation *abst)
355 {
356 struct cia_config *ccp = &cia_configuration;
357 bus_space_tag_t st;
358
359 switch (type) {
360 case ALPHA_BUS_TYPE_PCI_IO:
361 st = &ccp->cc_iot;
362 break;
363
364 case ALPHA_BUS_TYPE_PCI_MEM:
365 st = &ccp->cc_memt;
366 break;
367
368 default:
369 panic("cia_bus_get_window");
370 }
371
372 return (alpha_bus_space_get_window(st, window, abst));
373 }
374
375 void
376 cia_pyxis_intr_enable(int irq, int onoff)
377 {
378 uint64_t imask;
379 int s;
380
381 #if 0
382 printf("cia_pyxis_intr_enable: %s %d\n",
383 onoff ? "enabling" : "disabling", irq);
384 #endif
385
386 s = splhigh();
387 alpha_mb();
388 imask = REGVAL64(PYXIS_INT_MASK);
389 if (onoff)
390 imask |= (1UL << irq);
391 else
392 imask &= ~(1UL << irq);
393 REGVAL64(PYXIS_INT_MASK) = imask;
394 alpha_mb();
395 splx(s);
396 }
397