pci_550.c revision 1.40 1 1.40 thorpej /* $NetBSD: pci_550.c,v 1.40 2021/06/25 18:08:34 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.14 thorpej * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.3 ross * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.4 thorpej * NASA Ames Research Center, and by Andrew Gallatin.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.1 thorpej * Copyright (c) 1995, 1996 Carnegie-Mellon University.
35 1.1 thorpej * All rights reserved.
36 1.1 thorpej *
37 1.1 thorpej * Author: Chris G. Demetriou
38 1.36 matt *
39 1.1 thorpej * Permission to use, copy, modify and distribute this software and
40 1.1 thorpej * its documentation is hereby granted, provided that both the copyright
41 1.1 thorpej * notice and this permission notice appear in all copies of the
42 1.1 thorpej * software, derivative works or modified versions, and any portions
43 1.1 thorpej * thereof, and that both notices appear in supporting documentation.
44 1.36 matt *
45 1.36 matt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 1.36 matt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 1.1 thorpej * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 1.36 matt *
49 1.1 thorpej * Carnegie Mellon requests users of this software to return to
50 1.1 thorpej *
51 1.1 thorpej * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
52 1.1 thorpej * School of Computer Science
53 1.1 thorpej * Carnegie Mellon University
54 1.1 thorpej * Pittsburgh PA 15213-3890
55 1.1 thorpej *
56 1.1 thorpej * any improvements or extensions that they make and grant Carnegie the
57 1.1 thorpej * rights to redistribute these changes.
58 1.1 thorpej */
59 1.1 thorpej
60 1.1 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
61 1.1 thorpej
62 1.40 thorpej __KERNEL_RCSID(0, "$NetBSD: pci_550.c,v 1.40 2021/06/25 18:08:34 thorpej Exp $");
63 1.1 thorpej
64 1.1 thorpej #include <sys/types.h>
65 1.1 thorpej #include <sys/param.h>
66 1.1 thorpej #include <sys/time.h>
67 1.1 thorpej #include <sys/systm.h>
68 1.1 thorpej #include <sys/errno.h>
69 1.1 thorpej #include <sys/malloc.h>
70 1.1 thorpej #include <sys/device.h>
71 1.1 thorpej #include <sys/syslog.h>
72 1.1 thorpej
73 1.1 thorpej #include <machine/autoconf.h>
74 1.5 thorpej #include <machine/rpb.h>
75 1.1 thorpej
76 1.1 thorpej #include <dev/pci/pcireg.h>
77 1.1 thorpej #include <dev/pci/pcivar.h>
78 1.1 thorpej #include <dev/pci/pciidereg.h>
79 1.1 thorpej #include <dev/pci/pciidevar.h>
80 1.1 thorpej
81 1.1 thorpej #include <alpha/pci/ciareg.h>
82 1.1 thorpej #include <alpha/pci/ciavar.h>
83 1.1 thorpej
84 1.1 thorpej #include "sio.h"
85 1.1 thorpej #if NSIO
86 1.1 thorpej #include <alpha/pci/siovar.h>
87 1.1 thorpej #endif
88 1.1 thorpej
89 1.38 thorpej static int dec_550_intr_map(const struct pci_attach_args *,
90 1.38 thorpej pci_intr_handle_t *);
91 1.38 thorpej static const char *dec_550_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
92 1.38 thorpej char *, size_t);
93 1.38 thorpej static const struct evcnt *dec_550_intr_evcnt(pci_chipset_tag_t,
94 1.38 thorpej pci_intr_handle_t);
95 1.38 thorpej static void *dec_550_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
96 1.38 thorpej int, int (*func)(void *), void *);
97 1.38 thorpej static void dec_550_intr_disestablish(pci_chipset_tag_t, void *);
98 1.1 thorpej
99 1.1 thorpej #define DEC_550_PCI_IRQ_BEGIN 8
100 1.14 thorpej #define DEC_550_MAX_IRQ (64 - DEC_550_PCI_IRQ_BEGIN)
101 1.10 thorpej
102 1.10 thorpej /*
103 1.10 thorpej * The Miata has a Pyxis, which seems to have problems with stray
104 1.10 thorpej * interrupts. Work around this by just ignoring strays.
105 1.10 thorpej */
106 1.10 thorpej #define PCI_STRAY_MAX 0
107 1.1 thorpej
108 1.14 thorpej /*
109 1.14 thorpej * Some Miata models, notably models with a Cypress PCI-ISA bridge, have
110 1.14 thorpej * a PCI device (the OHCI USB controller) with interrupts tied to ISA IRQ
111 1.21 ross * lines. This IRQ is encoded as: line = FLAG | isa_irq. Usually FLAG
112 1.21 ross * is 0xe0, however, it can be 0xf0. We don't allow 0xf0 | irq15.
113 1.14 thorpej */
114 1.21 ross #define DEC_550_LINE_IS_ISA(line) ((line) >= 0xe0 && (line) <= 0xfe)
115 1.14 thorpej #define DEC_550_LINE_ISA_IRQ(line) ((line) & 0x0f)
116 1.14 thorpej
117 1.38 thorpej static void dec_550_intr_enable(pci_chipset_tag_t, int irq);
118 1.38 thorpej static void dec_550_intr_disable(pci_chipset_tag_t, int irq);
119 1.1 thorpej
120 1.39 thorpej static void
121 1.39 thorpej pci_550_pickintr(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
122 1.39 thorpej pci_chipset_tag_t pc)
123 1.1 thorpej {
124 1.16 thorpej char *cp;
125 1.1 thorpej int i;
126 1.1 thorpej
127 1.39 thorpej pc->pc_intr_v = core;
128 1.36 matt pc->pc_intr_map = dec_550_intr_map;
129 1.36 matt pc->pc_intr_string = dec_550_intr_string;
130 1.15 cgd pc->pc_intr_evcnt = dec_550_intr_evcnt;
131 1.36 matt pc->pc_intr_establish = dec_550_intr_establish;
132 1.36 matt pc->pc_intr_disestablish = dec_550_intr_disestablish;
133 1.1 thorpej
134 1.1 thorpej pc->pc_pciide_compat_intr_establish =
135 1.38 thorpej sio_pciide_compat_intr_establish;
136 1.1 thorpej
137 1.1 thorpej /*
138 1.1 thorpej * DEC 550's interrupts are enabled via the Pyxis interrupt
139 1.7 thorpej * mask register. Nothing to map.
140 1.1 thorpej */
141 1.1 thorpej
142 1.14 thorpej for (i = 0; i < DEC_550_MAX_IRQ; i++)
143 1.38 thorpej dec_550_intr_disable(pc, i);
144 1.1 thorpej
145 1.37 christos #define PCI_550_IRQ_STR 8
146 1.38 thorpej pc->pc_shared_intrs = alpha_shared_intr_alloc(DEC_550_MAX_IRQ,
147 1.37 christos PCI_550_IRQ_STR);
148 1.40 thorpej pc->pc_intr_desc = "dec 550";
149 1.38 thorpej pc->pc_vecbase = 0x900;
150 1.38 thorpej pc->pc_nirq = DEC_550_MAX_IRQ;
151 1.38 thorpej
152 1.38 thorpej pc->pc_intr_enable = dec_550_intr_enable;
153 1.38 thorpej pc->pc_intr_disable = dec_550_intr_disable;
154 1.38 thorpej
155 1.14 thorpej for (i = 0; i < DEC_550_MAX_IRQ; i++) {
156 1.38 thorpej alpha_shared_intr_set_maxstrays(pc->pc_shared_intrs, i,
157 1.14 thorpej PCI_STRAY_MAX);
158 1.39 thorpej alpha_shared_intr_set_private(pc->pc_shared_intrs, i, core);
159 1.16 thorpej
160 1.38 thorpej cp = alpha_shared_intr_string(pc->pc_shared_intrs, i);
161 1.37 christos snprintf(cp, PCI_550_IRQ_STR, "irq %d", i);
162 1.16 thorpej evcnt_attach_dynamic(alpha_shared_intr_evcnt(
163 1.38 thorpej pc->pc_shared_intrs, i), EVCNT_TYPE_INTR, NULL,
164 1.40 thorpej pc->pc_intr_desc, cp);
165 1.14 thorpej }
166 1.1 thorpej
167 1.1 thorpej #if NSIO
168 1.1 thorpej sio_intr_setup(pc, iot);
169 1.1 thorpej #endif
170 1.1 thorpej }
171 1.39 thorpej ALPHA_PCI_INTR_INIT(ST_DEC_550, pci_550_pickintr)
172 1.1 thorpej
173 1.38 thorpej static int
174 1.34 dyoung dec_550_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
175 1.1 thorpej {
176 1.19 sommerfe pcitag_t bustag = pa->pa_intrtag;
177 1.19 sommerfe int buspin = pa->pa_intrpin, line = pa->pa_intrline;
178 1.19 sommerfe pci_chipset_tag_t pc = pa->pa_pc;
179 1.1 thorpej int bus, device, function;
180 1.1 thorpej
181 1.1 thorpej if (buspin == 0) {
182 1.1 thorpej /* No IRQ used. */
183 1.1 thorpej return 1;
184 1.1 thorpej }
185 1.38 thorpej if (buspin < 0 || buspin > 4) {
186 1.1 thorpej printf("dec_550_intr_map: bad interrupt pin %d\n", buspin);
187 1.1 thorpej return 1;
188 1.1 thorpej }
189 1.1 thorpej
190 1.23 thorpej pci_decompose_tag(pc, bustag, &bus, &device, &function);
191 1.1 thorpej
192 1.1 thorpej /*
193 1.5 thorpej * There are two main variants of Miata: Miata 1 (Intel SIO)
194 1.5 thorpej * and Miata {1.5,2} (Cypress).
195 1.5 thorpej *
196 1.5 thorpej * The Miata 1 has a CMD PCI IDE wired to compatibility mode at
197 1.5 thorpej * device 4 of bus 0. This variant apparently also has the
198 1.5 thorpej * Pyxis DMA bug.
199 1.5 thorpej *
200 1.5 thorpej * On the Miata 1.5 and Miata 2, the Cypress PCI-ISA bridge lives
201 1.5 thorpej * on device 7 of bus 0. This device has PCI IDE wired to
202 1.5 thorpej * compatibility mode on functions 1 and 2.
203 1.5 thorpej *
204 1.5 thorpej * There will be no interrupt mapping for these devices, so just
205 1.5 thorpej * bail out now.
206 1.1 thorpej */
207 1.5 thorpej if (bus == 0) {
208 1.5 thorpej if ((hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
209 1.5 thorpej /* Miata 1 */
210 1.5 thorpej if (device == 7)
211 1.5 thorpej panic("dec_550_intr_map: SIO device");
212 1.5 thorpej else if (device == 4)
213 1.5 thorpej return (1);
214 1.5 thorpej } else {
215 1.5 thorpej /* Miata 1.5 or Miata 2 */
216 1.5 thorpej if (device == 7) {
217 1.5 thorpej if (function == 0)
218 1.5 thorpej panic("dec_550_intr_map: SIO device");
219 1.14 thorpej if (function == 1 || function == 2)
220 1.14 thorpej return (1);
221 1.5 thorpej }
222 1.5 thorpej }
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej /*
226 1.1 thorpej * The console places the interrupt mapping in the "line" value.
227 1.1 thorpej * A value of (char)-1 indicates there is no mapping.
228 1.1 thorpej */
229 1.1 thorpej if (line == 0xff) {
230 1.1 thorpej printf("dec_550_intr_map: no mapping for %d/%d/%d\n",
231 1.1 thorpej bus, device, function);
232 1.1 thorpej return (1);
233 1.1 thorpej }
234 1.1 thorpej
235 1.14 thorpej #if NSIO == 0
236 1.14 thorpej if (DEC_550_LINE_IS_ISA(line)) {
237 1.14 thorpej printf("dec_550_intr_map: ISA IRQ %d for %d/%d/%d\n",
238 1.14 thorpej DEC_550_LINE_ISA_IRQ(line), bus, device, function);
239 1.14 thorpej return (1);
240 1.14 thorpej }
241 1.14 thorpej #endif
242 1.1 thorpej
243 1.21 ross if (DEC_550_LINE_IS_ISA(line) == 0 && line >= DEC_550_MAX_IRQ) {
244 1.21 ross printf("dec_550_intr_map: irq %d out of range %d/%d/%d\n",
245 1.21 ross line, bus, device, function);
246 1.20 ross return (1);
247 1.21 ross }
248 1.38 thorpej alpha_pci_intr_handle_init(ihp, line, 0);
249 1.1 thorpej return (0);
250 1.1 thorpej }
251 1.1 thorpej
252 1.38 thorpej static const char *
253 1.38 thorpej dec_550_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
254 1.38 thorpej char * const buf, size_t const len)
255 1.1 thorpej {
256 1.38 thorpej #if NSIO
257 1.38 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
258 1.1 thorpej
259 1.38 thorpej if (DEC_550_LINE_IS_ISA(irq))
260 1.37 christos return sio_intr_string(NULL /*XXX*/,
261 1.38 thorpej DEC_550_LINE_ISA_IRQ(irq), buf, len);
262 1.14 thorpej #endif
263 1.14 thorpej
264 1.38 thorpej return alpha_pci_generic_intr_string(pc, ih, buf, len);
265 1.15 cgd }
266 1.15 cgd
267 1.38 thorpej static const struct evcnt *
268 1.38 thorpej dec_550_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
269 1.15 cgd {
270 1.38 thorpej #if NSIO
271 1.38 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
272 1.15 cgd
273 1.38 thorpej if (DEC_550_LINE_IS_ISA(irq))
274 1.15 cgd return (sio_intr_evcnt(NULL /*XXX*/,
275 1.38 thorpej DEC_550_LINE_ISA_IRQ(irq)));
276 1.15 cgd #endif
277 1.15 cgd
278 1.38 thorpej return alpha_pci_generic_intr_evcnt(pc, ih);
279 1.1 thorpej }
280 1.1 thorpej
281 1.38 thorpej static void *
282 1.38 thorpej dec_550_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
283 1.38 thorpej int const level, int (*func)(void *), void *arg)
284 1.1 thorpej {
285 1.38 thorpej #if NSIO
286 1.38 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
287 1.38 thorpej const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
288 1.1 thorpej
289 1.38 thorpej if (DEC_550_LINE_IS_ISA(irq))
290 1.14 thorpej return (sio_intr_establish(NULL /*XXX*/,
291 1.38 thorpej DEC_550_LINE_ISA_IRQ(irq), IST_LEVEL, level, flags,
292 1.38 thorpej func, arg));
293 1.14 thorpej #endif
294 1.14 thorpej
295 1.38 thorpej return alpha_pci_generic_intr_establish(pc, ih, level, func, arg);
296 1.1 thorpej }
297 1.1 thorpej
298 1.38 thorpej static void
299 1.38 thorpej dec_550_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
300 1.1 thorpej {
301 1.38 thorpej #if NSIO
302 1.38 thorpej struct alpha_shared_intrhand * const ih = cookie;
303 1.14 thorpej
304 1.14 thorpej /*
305 1.14 thorpej * We have to determine if this is an ISA IRQ or not! We do this
306 1.14 thorpej * by checking to see if the intrhand points back to an intrhead
307 1.14 thorpej * that points to our cia_config. If not, it's an ISA IRQ. Pretty
308 1.14 thorpej * disgusting, eh?
309 1.14 thorpej */
310 1.38 thorpej if (ih->ih_intrhead->intr_private != pc->pc_intr_v) {
311 1.14 thorpej sio_intr_disestablish(NULL /*XXX*/, cookie);
312 1.14 thorpej return;
313 1.14 thorpej }
314 1.14 thorpej #endif
315 1.36 matt
316 1.38 thorpej alpha_pci_generic_intr_disestablish(pc, cookie);
317 1.1 thorpej }
318 1.1 thorpej
319 1.38 thorpej static void
320 1.38 thorpej dec_550_intr_enable(pci_chipset_tag_t const pc __unused, int const irq)
321 1.1 thorpej {
322 1.14 thorpej cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 1);
323 1.1 thorpej }
324 1.1 thorpej
325 1.38 thorpej static void
326 1.38 thorpej dec_550_intr_disable(pci_chipset_tag_t const pc __unused, int const irq)
327 1.1 thorpej {
328 1.14 thorpej cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 0);
329 1.1 thorpej }
330