summaryrefslogtreecommitdiff
path: root/sys/dev/isa/opti.c
blob: fac35d2142437a033ad8043a5b8bddc425263ae9 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*	$OpenBSD: opti.c,v 1.5 1996/10/16 12:32:48 deraadt Exp $	*/

/*
 * Copyright (c) 1996 Michael Shalayeff
 * 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, 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Michael Shalayeff.
 * 4. 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 ``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 REGENTS 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.
 *
 */

/*
 * Code to setup 82C929 chipset
 */

/* #define	OPTI_DEBUG	9 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/device.h>

#include <machine/pio.h>

#include <i386/isa/icu.h>
#include <dev/isa/isavar.h>

#include <dev/isa/opti.h>

#ifdef OPTI_DEBUG
   int opti_debuglevel = OPTI_DEBUG;
#  define XDEBUG(level, data)	((opti_debuglevel >= level)? printf data:0)
#else
#  define XDEBUG(level, data)	/* ((opti_debuglevel >= level)? printf data:0) */
#endif

int	opti_type = OPTI_C929;	/* XXX only one card can be installed */

#define	OPTI_cd_valid_ift(i)	((i)==OPTI_SONY||(i)==OPTI_PANASONIC||\
					(i)==OPTI_MITSUMI||(i)==OPTI_IDE)

static __inline int OPTI_cd_addr __P((int));
static __inline int OPTI_cd_irq __P((int));
static __inline int OPTI_cd_drq __P((int));
static __inline int OPTI_snd_addr __P((int));
static __inline int OPTI_snd_irq __P((int));
static __inline int OPTI_snd_drq __P((int));
static __inline void opti_outb __P((u_short, u_char));
static __inline u_char opti_inb __P((u_short));
static int opti_present __P((void));

static __inline int
OPTI_cd_addr(a)
	int	a;
{
	switch(a) {
	case 0x320:
		return 0xc0;
	case 0x330:
		return 0x40;
	case 0x340:
		return 0x00;
	case 0x360:
		return 0x80;
	default:
		return -1;
	}
}

static __inline int
OPTI_cd_irq(i)
	int	i;
{
	switch(i) {
	case 5:
		return 0x04;
	case 7:
		return 0x08;
	case 3:
		return 0x0c;
	case 9:
		return 0x10;
	case 10:
		return 0x14;
	case 11:
		return 0x18;
	case -1:
		return 0x00;
	default:
		return -1;
	}
}

static __inline int
OPTI_cd_drq(d)
	int	d;
{
	switch(d) {
	case 3:
	case 5:
		return 0;
	case 6:
		return 1;
	case 7:
		return 2;
	default:
		return 3;
	}
}

#define	OPTI_snd_valid_ift(i)	((i)==OPTI_WSS||(i)==OPTI_SB)

static __inline int
OPTI_snd_addr(a)
	int	a;
{
	switch(a) {
	case 0x220:
		return 0x0;
	case 0x240:
		return 0x3;
	case 0x530:
		return 0x8;
	case 0xE80:
		return 0x9;
	case 0xF40:
		return 0xa;
	case 0x604:
		return 0xb;
	default:
		return -1;
	}
}

static __inline int
OPTI_snd_irq(i)
	int	i;
{
	switch(i) {
	case 5:
		return 0x04;
	case 7:
		return 0x08;
	case 3:
		return 0x0c;
	case 9:
		return 0x10;
	case 10:
		return 0x14;
	case 11:
		return 0x18;
	case -1:
		return 0x00;
	default:
		return -1;
	}
}

static __inline int
OPTI_snd_drq(d)
	int	d;
{
	switch(d) {
	case 3:
	case 5:
		return 0;
	case 6:
		return 1;
	case 7:
		return 2;
	default:
		return 3;
	}
}

static __inline void
opti_outb(port, byte)
	u_short port;
	u_char byte;
{
	outb( OPTI_PASSWD, opti_type );
	outb( port, byte );
}

static __inline u_char
opti_inb(port)
	u_short port;
{
	outb( OPTI_PASSWD, opti_type );
	return inb( port );
}

static int
opti_present()
{
	register u_char	a, b;
	int s = splhigh();

	a = opti_inb( OPTI_PASSWD );
	opti_outb( OPTI_PASSWD, 0x00 );
	b = opti_inb( OPTI_PASSWD );
	opti_outb( OPTI_PASSWD, a );

	if (b != 2) {
		opti_type = OPTI_C928;

		a = opti_inb( OPTI_PASSWD );
		opti_outb( OPTI_PASSWD, 0x00 );
		b = opti_inb( OPTI_PASSWD );
		opti_outb( OPTI_PASSWD, a );
	}

	splx(s);

	return b == 2;
}

int
opti_cd_setup(ift, addr, irq, drq)
	int	ift, addr, irq, drq;
{
	int	ret = 0;

	XDEBUG( 2, ("opti: do CD setup type=%u, addr=0x%x, irq=%d, drq=%d\n",
		    ift, addr, irq, drq));

	if( !opti_present() )
		XDEBUG( 2, ("opti: not present.\n"));
	else if( !OPTI_cd_valid_ift(ift) )
		XDEBUG( 2, ("opti: invalid CD-ROM interface type.\n"));
	else if( OPTI_cd_addr(addr) == -1)
		XDEBUG( 2, ("opti: illegal CD-ROM interface address.\n"));
	else if( OPTI_cd_irq(irq) == -1)
		XDEBUG( 2, ("opti: wrong CD-ROM irq number.\n"));
	else if( OPTI_cd_drq(drq) == -1)
		XDEBUG( 2, ("opti: bad CD_ROM drq number.\n"));
	else {
			/* so the setup */
		int s = splhigh();
		register u_char	a, b;

			/* set interface type */
		a = opti_inb( OPTI_IFTP );
		b = (opti_inb( OPTI_DATA ) & 0x20) | 3 ;
		opti_outb( OPTI_DATA,  b );
		opti_outb( OPTI_IFTP, (a & OPTI_SND_MASK) | 2 * ift );
		opti_outb( OPTI_ENBL, 0x80 );

			/* we don't need any additional setup for IDE CD-ROM */
		if( ift != OPTI_IDE )
		{
				/* set address */
			a = opti_inb( OPTI_DATA );
			opti_outb( OPTI_DATA, (a & 0x3f) |
			     (0x40 * OPTI_cd_addr(addr)) );

				/* set irq */
			if( irq != IRQUNK )
			{
				a = opti_inb( OPTI_DATA );
				opti_outb( OPTI_DATA,
					  (inb( OPTI_DATA ) & 0xe3) |
					  OPTI_cd_irq(irq) );
			}

				/* set drq */
			if( drq != DRQUNK )
			{
				a = opti_inb( OPTI_DATA );
				opti_outb( OPTI_DATA,
					  (inb( OPTI_DATA ) & 0xfc) |
					  OPTI_cd_drq(drq) );
			}
		}
		splx(s);
		DELAY(1000);
		ret = 1;
	}

	return ret;
}

int
opti_snd_setup(ift, addr, irq, drq)
	int	ift, addr, irq, drq;
{
	XDEBUG( 2, ("opti: do SND setup type=%u,addr=%x,irq=%d,drq=%d\n",
		    ift, addr, irq, drq));

	if( !opti_present() )
		XDEBUG( 2, ("opti: not present.\n"));
	else if( !OPTI_snd_valid_ift(ift) )
		XDEBUG( 2, ("opti: invalid SND interface type.\n"));
	else if( OPTI_snd_addr(addr) == -1)
		XDEBUG( 2, ("opti: illegal SND interface address.\n"));
	else if( OPTI_snd_irq(irq) == -1)
		XDEBUG( 2, ("opti: wrong SND irq number.\n"));
	else if( OPTI_snd_drq(drq) == -1)
		XDEBUG( 2, ("opti: bad SND drq number.\n"));
	else {
			/* so the setup */
		int s = splhigh();
		register u_char	a;

		if (ift == OPTI_WSS) {
			a = opti_inb(OPTI_IFTP);
			opti_outb(OPTI_IFTP, ((a & ~OPTI_SND_MASK)
				  | (OPTI_snd_addr(addr)*16)) + 1);
			opti_outb(OPTI_ENBL, 0x1a);
		}

		splx(s);
		DELAY(1000);
		return 1;
	}

	return 0;
}