xref: /openbsd-src/sys/arch/alpha/pci/pci_kn300.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /* $OpenBSD: pci_kn300.c,v 1.7 2015/07/26 05:09:44 miod Exp $ */
2 /* $NetBSD: pci_kn300.c,v 1.28 2005/12/11 12:16:17 christos Exp $ */
3 
4 /*
5  * Copyright (c) 1998 by Matthew Jacob
6  * NASA AMES Research Center.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice immediately at the beginning of the file, without modification,
14  *    this list of conditions, and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 #include <sys/syslog.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <machine/autoconf.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/ppbreg.h>
50 
51 #include <alpha/mcbus/mcbusvar.h>
52 #include <alpha/mcbus/mcbusreg.h>
53 #include <alpha/pci/mcpciareg.h>
54 #include <alpha/pci/mcpciavar.h>
55 #include <alpha/pci/pci_kn300.h>
56 
57 #include "sio.h"
58 #if NSIO > 0 || NPCEB > 0
59 #include <alpha/pci/siovar.h>
60 #endif
61 
62 int	dec_kn300_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
63 
64 const char *dec_kn300_intr_string(void *, pci_intr_handle_t);
65 void	*dec_kn300_intr_establish(void *, pci_intr_handle_t,
66 	    int, int (*func)(void *), void *, const char *);
67 void	dec_kn300_intr_disestablish(void *, void *);
68 
69 #define	KN300_PCEB_IRQ	16
70 #define	NPIN		4
71 
72 #define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
73 static int savirqs[NIRQ];
74 
75 static struct alpha_shared_intr *kn300_pci_intr;
76 
77 static struct mcpcia_config *mcpcia_eisaccp = NULL;
78 
79 void	kn300_iointr (void *, unsigned long);
80 void	kn300_enable_intr (struct mcpcia_config *, int);
81 void	kn300_disable_intr (struct mcpcia_config *, int);
82 
83 void
84 pci_kn300_pickintr(ccp, first)
85 	struct mcpcia_config *ccp;
86 	int first;
87 {
88 	pci_chipset_tag_t pc = &ccp->cc_pc;
89 
90 	if (first) {
91 		int g;
92 
93 		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
94 		for (g = 0; g < NIRQ; g++) {
95 			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
96 			savirqs[g] = (char) -1;
97 		}
98 	}
99 
100 	pc->pc_intr_v = ccp;
101 	pc->pc_intr_map = dec_kn300_intr_map;
102 	pc->pc_intr_string = dec_kn300_intr_string;
103 	pc->pc_intr_establish = dec_kn300_intr_establish;
104 	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
105 
106 	/* Not supported on KN300. */
107 	pc->pc_pciide_compat_intr_establish = NULL;
108 
109 	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
110 		mcpcia_eisaccp = ccp;
111 #if NSIO > 0 || NPCEB > 0
112 		sio_intr_setup(pc, &ccp->cc_iot);
113 		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
114 #endif
115 	}
116 }
117 
118 int
119 dec_kn300_intr_map(pa, ihp)
120 	struct pci_attach_args *pa;
121 	pci_intr_handle_t *ihp;
122 {
123 	pcitag_t bustag = pa->pa_intrtag;
124 	int buspin = pa->pa_intrpin;
125 	pci_chipset_tag_t pc = pa->pa_pc;
126 	struct mcpcia_config *ccp = (struct mcpcia_config *)pc->pc_intr_v;
127 	int device;
128 	int mcpcia_irq;
129 
130 	if (pa->pa_bridgetag) {
131 		buspin = PPB_INTERRUPT_SWIZZLE(pa->pa_rawintrpin,
132 		    pa->pa_device);
133 		if (pa->pa_bridgeih[buspin - 1] != 0) {
134 			*ihp = pa->pa_bridgeih[buspin - 1];
135 			return 0;
136 		}
137 
138 		return 1;
139 	}
140 
141 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
142 
143 	/*
144 	 * On MID 5 device 1 is the internal NCR 53c810.
145 	 */
146 	if (ccp->cc_mid == 5 && device == 1) {
147 		mcpcia_irq = 16;
148 	} else if (device >= 2 && device <= 5) {
149 		mcpcia_irq = (device - 2) * 4 + buspin - 1;
150 	} else {
151 		printf("dec_kn300_intr_map: don't know how to setup %d/%d/%d\n",
152 		    pa->pa_bus, pa->pa_device, pa->pa_function);
153 		return 1;
154 	}
155 
156 	/*
157 	 * handle layout:
158 	 *
159 	 *	Determine kn300 IRQ (encoded in SCB vector):
160 	 *	bits 0..1	buspin-1
161 	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
162 	 *	bits 5..7	MID-4
163 	 *	bits 8..10	7-GID
164 	 *
165 	 *	Software only:
166 	 *	bits 11-15	MCPCIA IRQ
167 	 */
168 	*ihp = (pci_intr_handle_t)
169 		(buspin - 1			    )	|
170 		((device & 0x7)			<< 2)	|
171 		((ccp->cc_mid - 4)		<< 5)	|
172 		((7 - ccp->cc_gid)		<< 8)	|
173 		(mcpcia_irq			<< 11);
174 
175 	return (0);
176 }
177 
178 const char *
179 dec_kn300_intr_string(ccv, ih)
180 	void *ccv;
181 	pci_intr_handle_t ih;
182 {
183 	static char irqstr[64];
184 	int irq;
185 
186 	irq = ih & 0x3ff;
187 	if (irq >= NIRQ)
188 		panic("dec_kn300_intr_string: bogus kn300 IRQ 0x%x", irq);
189 
190 	snprintf(irqstr, sizeof irqstr, "kn300 irq %d", irq);
191 
192 	return (irqstr);
193 }
194 
195 void *
196 dec_kn300_intr_establish(ccv, ih, level, func, arg, name)
197         void *ccv, *arg;
198         pci_intr_handle_t ih;
199         int level;
200         int (*func) (void *);
201 	const char *name;
202 {
203 	struct mcpcia_config *ccp = ccv;
204 	void *cookie;
205 	int irq;
206 
207 	irq = ih & 0x3ff;
208 	if (irq >= NIRQ)
209 		panic("dec_kn300_intr_establish: bogus kn300 IRQ 0x%x", irq);
210 
211 	cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
212 	    level, func, arg, "kn300 irq");
213 
214 	if (cookie != NULL &&
215 	    alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
216 		scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq),
217 		    kn300_iointr, NULL);
218 		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
219 		savirqs[irq] = (ih >> 11) & 0x1f;
220 		kn300_enable_intr(ccp, savirqs[irq]);
221 		alpha_mb();
222 	}
223 	return (cookie);
224 }
225 
226 void
227 dec_kn300_intr_disestablish(ccv, cookie)
228 	void *ccv, *cookie;
229 {
230 	panic("dec_kn300_intr_disestablish not implemented");
231 }
232 
233 void
234 kn300_iointr(arg, vec)
235 	void *arg;
236 	unsigned long vec;
237 {
238 	struct mcpcia_softc *mcp;
239 	u_long irq;
240 
241 	irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);
242 
243 	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
244 		/*
245 		 * Any claim of an interrupt at this level is a hint to
246 		 * reset the stray interrupt count- elsewise a slow leak
247 		 * over time will cause this level to be shutdown.
248 		 */
249 		alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
250 		return;
251 	}
252 
253 	/*
254 	 * If we haven't finished configuring yet, or there is no mcp
255 	 * registered for this level yet, just return.
256 	 */
257 	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
258 	if (mcp == NULL || mcp->mcpcia_cc == NULL)
259 		return;
260 
261 	/*
262 	 * We're getting an interrupt for a device we haven't enabled.
263 	 * We had better not try and use -1 to find the right bit to disable.
264 	 */
265 	if (savirqs[irq] == -1) {
266 		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
267 		return;
268 	}
269 
270 	/*
271 	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
272 	 * if we've reached the limit.
273 	 */
274 	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
275 	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
276 		return;
277 	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
278 }
279 
280 void
281 kn300_enable_intr(ccp, irq)
282 	struct mcpcia_config *ccp;
283 	int irq;
284 {
285 	alpha_mb();
286 	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
287 	alpha_mb();
288 }
289 
290 void
291 kn300_disable_intr(ccp, irq)
292 	struct mcpcia_config *ccp;
293 	int irq;
294 {
295 	alpha_mb();
296 	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
297 	alpha_mb();
298 }
299