xref: /netbsd-src/sys/arch/alpha/pci/pci_eb64plus.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /* $NetBSD: pci_eb64plus.c,v 1.11 2002/05/15 16:57:42 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Author: Chris G. Demetriou
45  *
46  * Permission to use, copy, modify and distribute this software and
47  * its documentation is hereby granted, provided that both the copyright
48  * notice and this permission notice appear in all copies of the
49  * software, derivative works or modified versions, and any portions
50  * thereof, and that both notices appear in supporting documentation.
51  *
52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55  *
56  * Carnegie Mellon requests users of this software to return to
57  *
58  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59  *  School of Computer Science
60  *  Carnegie Mellon University
61  *  Pittsburgh PA 15213-3890
62  *
63  * any improvements or extensions that they make and grant Carnegie the
64  * rights to redistribute these changes.
65  */
66 
67 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
68 
69 __KERNEL_RCSID(0, "$NetBSD: pci_eb64plus.c,v 1.11 2002/05/15 16:57:42 thorpej Exp $");
70 
71 #include <sys/types.h>
72 #include <sys/param.h>
73 #include <sys/time.h>
74 #include <sys/systm.h>
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <sys/device.h>
78 #include <sys/syslog.h>
79 
80 #include <uvm/uvm_extern.h>
81 
82 #include <machine/autoconf.h>
83 
84 #include <dev/pci/pcireg.h>
85 #include <dev/pci/pcivar.h>
86 
87 #include <alpha/pci/apecsreg.h>
88 #include <alpha/pci/apecsvar.h>
89 
90 #include <alpha/pci/pci_eb64plus.h>
91 
92 #include "sio.h"
93 #if NSIO
94 #include <alpha/pci/siovar.h>
95 #endif
96 
97 int	dec_eb64plus_intr_map __P((struct pci_attach_args *,
98 	    pci_intr_handle_t *));
99 const char *dec_eb64plus_intr_string __P((void *, pci_intr_handle_t));
100 const struct evcnt *dec_eb64plus_intr_evcnt __P((void *, pci_intr_handle_t));
101 void	*dec_eb64plus_intr_establish __P((void *, pci_intr_handle_t,
102 	    int, int (*func)(void *), void *));
103 void	dec_eb64plus_intr_disestablish __P((void *, void *));
104 
105 #define	EB64PLUS_MAX_IRQ	32
106 #define	PCI_STRAY_MAX		5
107 
108 struct alpha_shared_intr *eb64plus_pci_intr;
109 
110 bus_space_tag_t eb64plus_intrgate_iot;
111 bus_space_handle_t eb64plus_intrgate_ioh;
112 
113 void	eb64plus_iointr __P((void *arg, unsigned long vec));
114 extern void	eb64plus_intr_enable __P((int irq));  /* pci_eb64plus_intr.S */
115 extern void	eb64plus_intr_disable __P((int irq)); /* pci_eb64plus_intr.S */
116 
117 void
118 pci_eb64plus_pickintr(acp)
119 	struct apecs_config *acp;
120 {
121 	bus_space_tag_t iot = &acp->ac_iot;
122 	pci_chipset_tag_t pc = &acp->ac_pc;
123 	char *cp;
124 	int i;
125 
126         pc->pc_intr_v = acp;
127         pc->pc_intr_map = dec_eb64plus_intr_map;
128         pc->pc_intr_string = dec_eb64plus_intr_string;
129 	pc->pc_intr_evcnt = dec_eb64plus_intr_evcnt;
130         pc->pc_intr_establish = dec_eb64plus_intr_establish;
131         pc->pc_intr_disestablish = dec_eb64plus_intr_disestablish;
132 
133 	/* Not supported on the EB64+. */
134 	pc->pc_pciide_compat_intr_establish = NULL;
135 
136 	eb64plus_intrgate_iot = iot;
137 	if (bus_space_map(eb64plus_intrgate_iot, 0x804, 3, 0,
138 	    &eb64plus_intrgate_ioh) != 0)
139 		panic("pci_eb64plus_pickintr: couldn't map interrupt PLD");
140 	for (i = 0; i < EB64PLUS_MAX_IRQ; i++)
141 		eb64plus_intr_disable(i);
142 
143 	eb64plus_pci_intr = alpha_shared_intr_alloc(EB64PLUS_MAX_IRQ, 8);
144 	for (i = 0; i < EB64PLUS_MAX_IRQ; i++) {
145 		alpha_shared_intr_set_maxstrays(eb64plus_pci_intr, i,
146 			PCI_STRAY_MAX);
147 
148 		cp = alpha_shared_intr_string(eb64plus_pci_intr, i);
149 		sprintf(cp, "irq %d", i);
150 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
151 		    eb64plus_pci_intr, i), EVCNT_TYPE_INTR, NULL,
152 		    "eb64+", cp);
153 	}
154 
155 #if NSIO
156 	sio_intr_setup(pc, iot);
157 #endif
158 }
159 
160 int
161 dec_eb64plus_intr_map(pa, ihp)
162 	struct pci_attach_args *pa;
163         pci_intr_handle_t *ihp;
164 {
165 	pcitag_t bustag = pa->pa_intrtag;
166 	int buspin = pa->pa_intrpin, line = pa->pa_intrline;
167 	pci_chipset_tag_t pc = pa->pa_pc;
168 	int bus, device, function;
169 
170 	if (buspin == 0) {
171 		/* No IRQ used. */
172 		return 1;
173 	}
174 	if (buspin > 4) {
175 		printf("dec_eb64plus_intr_map: bad interrupt pin %d\n", buspin);
176 		return 1;
177 	}
178 
179 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
180 
181 	/*
182 	 * The console places the interrupt mapping in the "line" value.
183 	 * A value of (char)-1 indicates there is no mapping.
184 	 */
185 	if (line == 0xff) {
186 		printf("dec_eb64plus_intr_map: no mapping for %d/%d/%d\n",
187 		    bus, device, function);
188 		return (1);
189 	}
190 
191 	if (line >= EB64PLUS_MAX_IRQ)
192 		panic("dec_eb64plus_intr_map: eb64+ irq too large (%d)\n",
193 		    line);
194 
195 	*ihp = line;
196 	return (0);
197 }
198 
199 const char *
200 dec_eb64plus_intr_string(acv, ih)
201 	void *acv;
202 	pci_intr_handle_t ih;
203 {
204         static char irqstr[15];          /* 11 + 2 + NULL + sanity */
205 
206         if (ih > EB64PLUS_MAX_IRQ)
207                 panic("dec_eb64plus_intr_string: bogus eb64+ IRQ 0x%lx\n", ih);
208         sprintf(irqstr, "eb64+ irq %ld", ih);
209         return (irqstr);
210 }
211 
212 const struct evcnt *
213 dec_eb64plus_intr_evcnt(acv, ih)
214 	void *acv;
215 	pci_intr_handle_t ih;
216 {
217 
218 	if (ih > EB64PLUS_MAX_IRQ)
219 		panic("dec_eb64plus_intr_string: bogus eb64+ IRQ 0x%lx\n", ih);
220 	return (alpha_shared_intr_evcnt(eb64plus_pci_intr, ih));
221 }
222 
223 void *
224 dec_eb64plus_intr_establish(acv, ih, level, func, arg)
225         void *acv, *arg;
226         pci_intr_handle_t ih;
227         int level;
228         int (*func) __P((void *));
229 {
230 	void *cookie;
231 
232 	if (ih > EB64PLUS_MAX_IRQ)
233 		panic("dec_eb64plus_intr_establish: bogus eb64+ IRQ 0x%lx\n",
234 		    ih);
235 
236 	cookie = alpha_shared_intr_establish(eb64plus_pci_intr, ih, IST_LEVEL,
237 	    level, func, arg, "eb64+ irq");
238 
239 	if (cookie != NULL &&
240 	    alpha_shared_intr_firstactive(eb64plus_pci_intr, ih)) {
241 		scb_set(0x900 + SCB_IDXTOVEC(ih), eb64plus_iointr, NULL);
242 		eb64plus_intr_enable(ih);
243 	}
244 	return (cookie);
245 }
246 
247 void
248 dec_eb64plus_intr_disestablish(acv, cookie)
249         void *acv, *cookie;
250 {
251 	struct alpha_shared_intrhand *ih = cookie;
252 	unsigned int irq = ih->ih_num;
253 	int s;
254 
255 	s = splhigh();
256 
257 	alpha_shared_intr_disestablish(eb64plus_pci_intr, cookie,
258 	    "eb64+ irq");
259 	if (alpha_shared_intr_isactive(eb64plus_pci_intr, irq) == 0) {
260 		eb64plus_intr_disable(irq);
261 		alpha_shared_intr_set_dfltsharetype(eb64plus_pci_intr, irq,
262 		    IST_NONE);
263 		scb_free(0x900 + SCB_IDXTOVEC(irq));
264 	}
265 
266 	splx(s);
267 }
268 
269 void
270 eb64plus_iointr(arg, vec)
271 	void *arg;
272 	unsigned long vec;
273 {
274 	int irq;
275 
276 	irq = SCB_VECTOIDX(vec - 0x900);
277 
278 	if (!alpha_shared_intr_dispatch(eb64plus_pci_intr, irq)) {
279 		alpha_shared_intr_stray(eb64plus_pci_intr, irq,
280 		    "eb64+ irq");
281 		if (ALPHA_SHARED_INTR_DISABLE(eb64plus_pci_intr, irq))
282 			eb64plus_intr_disable(irq);
283 	}
284 }
285 
286 #if 0		/* THIS DOES NOT WORK!  see pci_eb64plus_intr.S. */
287 u_int8_t eb64plus_intr_mask[3] = { 0xff, 0xff, 0xff };
288 
289 void
290 eb64plus_intr_enable(irq)
291 	int irq;
292 {
293 	int byte = (irq / 8), bit = (irq % 8);
294 
295 #if 1
296 	printf("eb64plus_intr_enable: enabling %d (%d:%d)\n", irq, byte, bit);
297 #endif
298 	eb64plus_intr_mask[byte] &= ~(1 << bit);
299 
300 	bus_space_write_1(eb64plus_intrgate_iot, eb64plus_intrgate_ioh, byte,
301 	    eb64plus_intr_mask[byte]);
302 }
303 
304 void
305 eb64plus_intr_disable(irq)
306 	int irq;
307 {
308 	int byte = (irq / 8), bit = (irq % 8);
309 
310 #if 1
311 	printf("eb64plus_intr_disable: disabling %d (%d:%d)\n", irq, byte, bit);
312 #endif
313 	eb64plus_intr_mask[byte] |= (1 << bit);
314 
315 	bus_space_write_1(eb64plus_intrgate_iot, eb64plus_intrgate_ioh, byte,
316 	    eb64plus_intr_mask[byte]);
317 }
318 #endif
319