summaryrefslogtreecommitdiff
path: root/sys/arch/alpha/pci/pci_kn300.c
blob: 60e8a88a925d107f782c005ae7f11f5efc7d7551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* $OpenBSD: pci_kn300.c,v 1.1 2007/03/16 21:22:27 robert Exp $ */
/* $NetBSD: pci_kn300.c,v 1.28 2005/12/11 12:16:17 christos Exp $ */

/*
 * Copyright (c) 1998 by Matthew Jacob
 * NASA AMES Research Center.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/syslog.h>

#include <uvm/uvm_extern.h>

#include <machine/autoconf.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <alpha/mcbus/mcbusvar.h>
#include <alpha/mcbus/mcbusreg.h>
#include <alpha/pci/mcpciareg.h>
#include <alpha/pci/mcpciavar.h>
#include <alpha/pci/pci_kn300.h>

#include "sio.h"
#if NSIO > 0 || NPCEB > 0
#include <alpha/pci/siovar.h>
#endif

int	dec_kn300_intr_map (void *, pcitag_t, int, int, pci_intr_handle_t *);

const char *dec_kn300_intr_string (void *, pci_intr_handle_t);
void	*dec_kn300_intr_establish (void *, pci_intr_handle_t,
	    int, int (*func)(void *), void *, char *);
void	dec_kn300_intr_disestablish (void *, void *);

#define	KN300_PCEB_IRQ	16
#define	NPIN		4

#define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
static int savirqs[NIRQ];

static struct alpha_shared_intr *kn300_pci_intr;

static struct mcpcia_config *mcpcia_eisaccp = NULL;

void	kn300_iointr (void *, unsigned long);
void	kn300_enable_intr (struct mcpcia_config *, int);
void	kn300_disable_intr (struct mcpcia_config *, int);

void
pci_kn300_pickintr(ccp, first)
	struct mcpcia_config *ccp;
	int first;
{
	pci_chipset_tag_t pc = &ccp->cc_pc;

	if (first) {
		int g;

		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
		for (g = 0; g < NIRQ; g++) {
			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
			savirqs[g] = (char) -1;
		}
	}

	pc->pc_intr_v = ccp;
	pc->pc_intr_map = dec_kn300_intr_map;
	pc->pc_intr_string = dec_kn300_intr_string;
	pc->pc_intr_establish = dec_kn300_intr_establish;
	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;

	/* Not supported on KN300. */
	pc->pc_pciide_compat_intr_establish = NULL;

	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
		mcpcia_eisaccp = ccp;
#if NSIO > 0 || NPCEB > 0
		sio_intr_setup(pc, &ccp->cc_iot);
		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
#endif
	}
}

int     
dec_kn300_intr_map(ccv, bustag, buspin, line, ihp)
	void *ccv;
	pcitag_t bustag;
	int buspin, line;
	pci_intr_handle_t *ihp;
{
	struct mcpcia_config *ccp = ccv; 
	pci_chipset_tag_t pc = &ccp->cc_pc;
	int device;
	int mcpcia_irq;

	if (buspin == 0) {
		/* No IRQ used. */
		return 1;
	}
	if (buspin > 4 || buspin < 0) {
		printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
		return 1;
	}

	pci_decompose_tag(pc, bustag, NULL, &device, NULL);

	/*
	 * On MID 5 device 1 is the internal NCR 53c810.
	 */
	if (ccp->cc_mid == 5 && device == 1) {
		mcpcia_irq = 16;
	} else if (device >= 2 && device <= 5) {
		mcpcia_irq = (device - 2) * 4 + buspin - 1;
	} else {
		printf("dec_kn300_intr_map: weird device number %d\n", device);
		return(1);
	}

	/*
	 * handle layout:
	 *
	 *	Determine kn300 IRQ (encoded in SCB vector):
	 *	bits 0..1	buspin-1
	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
	 *	bits 5..7	MID-4
	 *	bits 8..10	7-GID
	 *
	 *	Software only:
	 *	bits 11-15	MCPCIA IRQ
	 */
	*ihp = (pci_intr_handle_t)
		(buspin - 1			    )	|
		((device & 0x7)			<< 2)	|
		((ccp->cc_mid - 4)		<< 5)	|
		((7 - ccp->cc_gid)		<< 8)	|
		(mcpcia_irq			<< 11);
	return (0);
}

const char *
dec_kn300_intr_string(ccv, ih)
	void *ccv;
	pci_intr_handle_t ih;
{
	static char irqstr[64];
	int irq;

	irq = ih & 0x3ff;
	if (irq > NIRQ)
		panic("dec_kn300_intr_string: bogus kn300 IRQ 0x%x", irq);

	snprintf(irqstr, sizeof irqstr, "kn300 irq %ld", irq);

	return (irqstr);
}

void *
dec_kn300_intr_establish(ccv, ih, level, func, arg, name)
        void *ccv, *arg;
        pci_intr_handle_t ih;
        int level;
        int (*func) (void *);
	char *name;
{           
	struct mcpcia_config *ccp = ccv;
	void *cookie;
	int irq;

	irq = ih & 0x3ff;
	if (irq > NIRQ)
		panic("dec_kn300_intr_establish: bogus kn300 IRQ 0x%x", irq);

	cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
	    level, func, arg, "kn300 irq");

	if (cookie != NULL &&
	    alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
		scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq),
		    kn300_iointr, NULL);
		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
		savirqs[irq] = (ih >> 11) & 0x1f;
		kn300_enable_intr(ccp, savirqs[irq]);
		alpha_mb();
	}
	return (cookie);
}

void    
dec_kn300_intr_disestablish(ccv, cookie)
	void *ccv, *cookie;
{
	panic("dec_kn300_intr_disestablish not implemented");
}

void
kn300_iointr(arg, vec)
	void *arg;
	unsigned long vec;
{
	struct mcpcia_softc *mcp;
	u_long irq;

	irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);

	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
		/*
		 * Any claim of an interrupt at this level is a hint to
		 * reset the stray interrupt count- elsewise a slow leak
		 * over time will cause this level to be shutdown.
		 */
		alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
		return;
	}

	/*
	 * If we haven't finished configuring yet, or there is no mcp
	 * registered for this level yet, just return.
	 */
	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
	if (mcp == NULL || mcp->mcpcia_cc == NULL)
		return;

	/*
	 * We're getting an interrupt for a device we haven't enabled.
	 * We had better not try and use -1 to find the right bit to disable.
	 */
	if (savirqs[irq] == -1) {
		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
		return;
	}

	/*
	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
	 * if we've reached the limit.
	 */
	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
		return;
	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
}

void
kn300_enable_intr(ccp, irq)
	struct mcpcia_config *ccp;
	int irq;
{
	alpha_mb();
	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
	alpha_mb();
}

void
kn300_disable_intr(ccp, irq)
	struct mcpcia_config *ccp;
	int irq;
{
	alpha_mb();
	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
	alpha_mb();
}