xref: /openbsd/sys/arch/alpha/pci/pci_kn300.c (revision a6445c1d)
1 /* $OpenBSD: pci_kn300.c,v 1.6 2014/05/08 20:46:49 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 
50 #include <alpha/mcbus/mcbusvar.h>
51 #include <alpha/mcbus/mcbusreg.h>
52 #include <alpha/pci/mcpciareg.h>
53 #include <alpha/pci/mcpciavar.h>
54 #include <alpha/pci/pci_kn300.h>
55 
56 #include "sio.h"
57 #if NSIO > 0 || NPCEB > 0
58 #include <alpha/pci/siovar.h>
59 #endif
60 
61 int	dec_kn300_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
62 
63 const char *dec_kn300_intr_string(void *, pci_intr_handle_t);
64 void	*dec_kn300_intr_establish(void *, pci_intr_handle_t,
65 	    int, int (*func)(void *), void *, const char *);
66 void	dec_kn300_intr_disestablish(void *, void *);
67 
68 #define	KN300_PCEB_IRQ	16
69 #define	NPIN		4
70 
71 #define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
72 static int savirqs[NIRQ];
73 
74 static struct alpha_shared_intr *kn300_pci_intr;
75 
76 static struct mcpcia_config *mcpcia_eisaccp = NULL;
77 
78 void	kn300_iointr (void *, unsigned long);
79 void	kn300_enable_intr (struct mcpcia_config *, int);
80 void	kn300_disable_intr (struct mcpcia_config *, int);
81 
82 void
83 pci_kn300_pickintr(ccp, first)
84 	struct mcpcia_config *ccp;
85 	int first;
86 {
87 	pci_chipset_tag_t pc = &ccp->cc_pc;
88 
89 	if (first) {
90 		int g;
91 
92 		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
93 		for (g = 0; g < NIRQ; g++) {
94 			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
95 			savirqs[g] = (char) -1;
96 		}
97 	}
98 
99 	pc->pc_intr_v = ccp;
100 	pc->pc_intr_map = dec_kn300_intr_map;
101 	pc->pc_intr_string = dec_kn300_intr_string;
102 	pc->pc_intr_establish = dec_kn300_intr_establish;
103 	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
104 
105 	/* Not supported on KN300. */
106 	pc->pc_pciide_compat_intr_establish = NULL;
107 
108 	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
109 		mcpcia_eisaccp = ccp;
110 #if NSIO > 0 || NPCEB > 0
111 		sio_intr_setup(pc, &ccp->cc_iot);
112 		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
113 #endif
114 	}
115 }
116 
117 int
118 dec_kn300_intr_map(pa, ihp)
119 	struct pci_attach_args *pa;
120 	pci_intr_handle_t *ihp;
121 {
122 	pcitag_t bustag = pa->pa_intrtag;
123 	int buspin = pa->pa_intrpin;
124 	pci_chipset_tag_t pc = pa->pa_pc;
125 	struct mcpcia_config *ccp = (struct mcpcia_config *)pc->pc_intr_v;
126 	int device;
127 	int mcpcia_irq;
128 
129 	if (buspin == 0) {
130 		/* No IRQ used. */
131 		return 1;
132 	}
133 	if (buspin > 4 || buspin < 0) {
134 		printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
135 		return 1;
136 	}
137 
138 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
139 
140 	/*
141 	 * On MID 5 device 1 is the internal NCR 53c810.
142 	 */
143 	if (ccp->cc_mid == 5 && device == 1) {
144 		mcpcia_irq = 16;
145 	} else if (device >= 2 && device <= 5) {
146 		mcpcia_irq = (device - 2) * 4 + buspin - 1;
147 	} else {
148 		printf("dec_kn300_intr_map: weird device number %d\n", device);
149 		return(1);
150 	}
151 
152 	/*
153 	 * handle layout:
154 	 *
155 	 *	Determine kn300 IRQ (encoded in SCB vector):
156 	 *	bits 0..1	buspin-1
157 	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
158 	 *	bits 5..7	MID-4
159 	 *	bits 8..10	7-GID
160 	 *
161 	 *	Software only:
162 	 *	bits 11-15	MCPCIA IRQ
163 	 */
164 	*ihp = (pci_intr_handle_t)
165 		(buspin - 1			    )	|
166 		((device & 0x7)			<< 2)	|
167 		((ccp->cc_mid - 4)		<< 5)	|
168 		((7 - ccp->cc_gid)		<< 8)	|
169 		(mcpcia_irq			<< 11);
170 	return (0);
171 }
172 
173 const char *
174 dec_kn300_intr_string(ccv, ih)
175 	void *ccv;
176 	pci_intr_handle_t ih;
177 {
178 	static char irqstr[64];
179 	int irq;
180 
181 	irq = ih & 0x3ff;
182 	if (irq >= NIRQ)
183 		panic("dec_kn300_intr_string: bogus kn300 IRQ 0x%x", irq);
184 
185 	snprintf(irqstr, sizeof irqstr, "kn300 irq %d", irq);
186 
187 	return (irqstr);
188 }
189 
190 void *
191 dec_kn300_intr_establish(ccv, ih, level, func, arg, name)
192         void *ccv, *arg;
193         pci_intr_handle_t ih;
194         int level;
195         int (*func) (void *);
196 	const char *name;
197 {
198 	struct mcpcia_config *ccp = ccv;
199 	void *cookie;
200 	int irq;
201 
202 	irq = ih & 0x3ff;
203 	if (irq >= NIRQ)
204 		panic("dec_kn300_intr_establish: bogus kn300 IRQ 0x%x", irq);
205 
206 	cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
207 	    level, func, arg, "kn300 irq");
208 
209 	if (cookie != NULL &&
210 	    alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
211 		scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq),
212 		    kn300_iointr, NULL);
213 		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
214 		savirqs[irq] = (ih >> 11) & 0x1f;
215 		kn300_enable_intr(ccp, savirqs[irq]);
216 		alpha_mb();
217 	}
218 	return (cookie);
219 }
220 
221 void
222 dec_kn300_intr_disestablish(ccv, cookie)
223 	void *ccv, *cookie;
224 {
225 	panic("dec_kn300_intr_disestablish not implemented");
226 }
227 
228 void
229 kn300_iointr(arg, vec)
230 	void *arg;
231 	unsigned long vec;
232 {
233 	struct mcpcia_softc *mcp;
234 	u_long irq;
235 
236 	irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);
237 
238 	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
239 		/*
240 		 * Any claim of an interrupt at this level is a hint to
241 		 * reset the stray interrupt count- elsewise a slow leak
242 		 * over time will cause this level to be shutdown.
243 		 */
244 		alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
245 		return;
246 	}
247 
248 	/*
249 	 * If we haven't finished configuring yet, or there is no mcp
250 	 * registered for this level yet, just return.
251 	 */
252 	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
253 	if (mcp == NULL || mcp->mcpcia_cc == NULL)
254 		return;
255 
256 	/*
257 	 * We're getting an interrupt for a device we haven't enabled.
258 	 * We had better not try and use -1 to find the right bit to disable.
259 	 */
260 	if (savirqs[irq] == -1) {
261 		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
262 		return;
263 	}
264 
265 	/*
266 	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
267 	 * if we've reached the limit.
268 	 */
269 	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
270 	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
271 		return;
272 	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
273 }
274 
275 void
276 kn300_enable_intr(ccp, irq)
277 	struct mcpcia_config *ccp;
278 	int irq;
279 {
280 	alpha_mb();
281 	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
282 	alpha_mb();
283 }
284 
285 void
286 kn300_disable_intr(ccp, irq)
287 	struct mcpcia_config *ccp;
288 	int irq;
289 {
290 	alpha_mb();
291 	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
292 	alpha_mb();
293 }
294