1 /*
2  * sound/opl3sa.c
3  *
4  * Low level driver for Yamaha YMF701B aka OPL3-SA chip
5  *
6  *
7  *
8  * Copyright (C) by Hannu Savolainen 1993-1997
9  *
10  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
11  * Version 2 (June 1991). See the "COPYING" file distributed with this software
12  * for more info.
13  *
14  * Changes:
15  *	Alan Cox		Modularisation
16  *	Christoph Hellwig	Adapted to module_init/module_exit
17  *	Arnaldo C. de Melo	got rid of attach_uart401
18  *
19  * FIXME:
20  * 	Check for install of mpu etc is wrong, should check result of the mss stuff
21  */
22 
23 #include <linux/init.h>
24 #include <linux/module.h>
25 
26 #undef  SB_OK
27 
28 #include "sound_config.h"
29 
30 #include "ad1848.h"
31 #include "mpu401.h"
32 
33 #ifdef SB_OK
34 #include "sb.h"
35 static int sb_initialized = 0;
36 #endif
37 
38 static int kilroy_was_here = 0;	/* Don't detect twice */
39 static int mpu_initialized = 0;
40 
41 static int *opl3sa_osp = NULL;
42 
opl3sa_read(int addr)43 static unsigned char opl3sa_read(int addr)
44 {
45 	unsigned long flags;
46 	unsigned char tmp;
47 
48 	save_flags(flags);
49 	cli();
50 	outb((0x1d), 0xf86);	/* password */
51 	outb(((unsigned char) addr), 0xf86);	/* address */
52 	tmp = inb(0xf87);	/* data */
53 	restore_flags(flags);
54 
55 	return tmp;
56 }
57 
opl3sa_write(int addr,int data)58 static void opl3sa_write(int addr, int data)
59 {
60 	unsigned long flags;
61 
62 	save_flags(flags);
63 	cli();
64 	outb((0x1d), 0xf86);	/* password */
65 	outb(((unsigned char) addr), 0xf86);	/* address */
66 	outb(((unsigned char) data), 0xf87);	/* data */
67 	restore_flags(flags);
68 }
69 
opl3sa_detect(void)70 static int __init opl3sa_detect(void)
71 {
72 	int tmp;
73 
74 	if (((tmp = opl3sa_read(0x01)) & 0xc4) != 0x04)
75 	{
76 		DDB(printk("OPL3-SA detect error 1 (%x)\n", opl3sa_read(0x01)));
77 		/* return 0; */
78 	}
79 
80 	/*
81 	 * Check that the password feature has any effect
82 	 */
83 
84 	if (inb(0xf87) == tmp)
85 	{
86 		DDB(printk("OPL3-SA detect failed 2 (%x/%x)\n", tmp, inb(0xf87)));
87 		return 0;
88 	}
89 	tmp = (opl3sa_read(0x04) & 0xe0) >> 5;
90 
91 	if (tmp != 0 && tmp != 1)
92 	{
93 		DDB(printk("OPL3-SA detect failed 3 (%d)\n", tmp));
94 		return 0;
95 	}
96 	DDB(printk("OPL3-SA mode %x detected\n", tmp));
97 
98 	opl3sa_write(0x01, 0x00);	/* Disable MSS */
99 	opl3sa_write(0x02, 0x00);	/* Disable SB */
100 	opl3sa_write(0x03, 0x00);	/* Disable MPU */
101 
102 	return 1;
103 }
104 
105 /*
106  *    Probe and attach routines for the Windows Sound System mode of
107  *     OPL3-SA
108  */
109 
probe_opl3sa_wss(struct address_info * hw_config)110 static int __init probe_opl3sa_wss(struct address_info *hw_config)
111 {
112 	int ret;
113 	unsigned char tmp = 0x24;	/* WSS enable */
114 
115 	if (check_region(0xf86, 2))	/* Control port is busy */
116 		return 0;
117 	/*
118 	 * Check if the IO port returns valid signature. The original MS Sound
119 	 * system returns 0x04 while some cards (OPL3-SA for example)
120 	 * return 0x00.
121 	 */
122 
123 	if (check_region(hw_config->io_base, 8))
124 	{
125 		printk(KERN_ERR "OPL3-SA: MSS I/O port conflict (%x)\n", hw_config->io_base);
126 		return 0;
127 	}
128 	opl3sa_osp = hw_config->osp;
129 
130 	if (!opl3sa_detect())
131 	{
132 		printk(KERN_ERR "OSS: OPL3-SA chip not found\n");
133 		return 0;
134 	}
135 
136 	switch (hw_config->io_base)
137 	{
138 		case 0x530:
139 			tmp |= 0x00;
140 			break;
141 		case 0xe80:
142 			tmp |= 0x08;
143 			break;
144 		case 0xf40:
145 			tmp |= 0x10;
146 			break;
147 		case 0x604:
148 			tmp |= 0x18;
149 			break;
150 		default:
151 			printk(KERN_ERR "OSS: Unsupported OPL3-SA/WSS base %x\n", hw_config->io_base);
152 		  return 0;
153 	}
154 
155 	opl3sa_write(0x01, tmp);	/* WSS setup register */
156 	kilroy_was_here = 1;
157 
158 	ret = probe_ms_sound(hw_config);
159 	if (ret)
160 		request_region(0xf86, 2, "OPL3-SA");
161 
162 	return ret;
163 }
164 
attach_opl3sa_wss(struct address_info * hw_config)165 static void __init attach_opl3sa_wss(struct address_info *hw_config)
166 {
167 	int nm = num_mixers;
168 
169 	/* FIXME */
170 	attach_ms_sound(hw_config, THIS_MODULE);
171 	if (num_mixers > nm)	/* A mixer was installed */
172 	{
173 		AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
174 		AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
175 		AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
176 	}
177 }
178 
179 
probe_opl3sa_mpu(struct address_info * hw_config)180 static int __init probe_opl3sa_mpu(struct address_info *hw_config)
181 {
182 	unsigned char conf;
183 	static signed char irq_bits[] = {
184 		-1, -1, -1, -1, -1, 1, -1, 2, -1, 3, 4
185 	};
186 
187 	if (!kilroy_was_here)
188 		return 0;	/* OPL3-SA has not been detected earlier */
189 
190 	if (mpu_initialized)
191 	{
192 		DDB(printk("OPL3-SA: MPU mode already initialized\n"));
193 		return 0;
194 	}
195 	if (hw_config->irq > 10)
196 	{
197 		printk(KERN_ERR "OPL3-SA: Bad MPU IRQ %d\n", hw_config->irq);
198 		return 0;
199 	}
200 	if (irq_bits[hw_config->irq] == -1)
201 	{
202 		printk(KERN_ERR "OPL3-SA: Bad MPU IRQ %d\n", hw_config->irq);
203 		return 0;
204 	}
205 	switch (hw_config->io_base)
206 	{
207 		case 0x330:
208 			conf = 0x00;
209 			break;
210 		case 0x332:
211 			conf = 0x20;
212 			break;
213 		case 0x334:
214 			conf = 0x40;
215 			break;
216 		case 0x300:
217 			conf = 0x60;
218 			break;
219 		default:
220 			return 0;	/* Invalid port */
221 	}
222 
223 	conf |= 0x83;		/* MPU & OPL3 (synth) & game port enable */
224 	conf |= irq_bits[hw_config->irq] << 2;
225 
226 	opl3sa_write(0x03, conf);
227 
228 	mpu_initialized = 1;
229 	hw_config->name = "OPL3-SA (MPU401)";
230 
231 	return probe_uart401(hw_config, THIS_MODULE);
232 }
233 
unload_opl3sa_wss(struct address_info * hw_config)234 static void __exit unload_opl3sa_wss(struct address_info *hw_config)
235 {
236 	int dma2 = hw_config->dma2;
237 
238 	if (dma2 == -1)
239 		dma2 = hw_config->dma;
240 
241 	release_region(0xf86, 2);
242 	release_region(hw_config->io_base, 4);
243 
244 	ad1848_unload(hw_config->io_base + 4,
245 		      hw_config->irq,
246 		      hw_config->dma,
247 		      dma2,
248 		      0);
249 	sound_unload_audiodev(hw_config->slots[0]);
250 }
251 
unload_opl3sa_mpu(struct address_info * hw_config)252 static inline void __exit unload_opl3sa_mpu(struct address_info *hw_config)
253 {
254 	unload_uart401(hw_config);
255 }
256 
257 #ifdef SB_OK
unload_opl3sa_sb(struct address_info * hw_config)258 static inline void __exit unload_opl3sa_sb(struct address_info *hw_config)
259 {
260 	sb_dsp_unload(hw_config);
261 }
262 #endif
263 
264 static int found_mpu;
265 
266 static struct address_info cfg;
267 static struct address_info cfg_mpu;
268 
269 static int __initdata io	= -1;
270 static int __initdata irq	= -1;
271 static int __initdata dma	= -1;
272 static int __initdata dma2	= -1;
273 static int __initdata mpu_io	= -1;
274 static int __initdata mpu_irq	= -1;
275 
276 MODULE_PARM(io,"i");
277 MODULE_PARM(irq,"i");
278 MODULE_PARM(dma,"i");
279 MODULE_PARM(dma2,"i");
280 MODULE_PARM(mpu_io,"i");
281 MODULE_PARM(mpu_irq,"i");
282 
init_opl3sa(void)283 static int __init init_opl3sa(void)
284 {
285 	if (io == -1 || irq == -1 || dma == -1) {
286 		printk(KERN_ERR "opl3sa: dma, irq and io must be set.\n");
287 		return -EINVAL;
288 	}
289 
290 	cfg.io_base = io;
291 	cfg.irq = irq;
292 	cfg.dma = dma;
293 	cfg.dma2 = dma2;
294 
295 	cfg_mpu.io_base = mpu_io;
296 	cfg_mpu.irq = mpu_irq;
297 
298 	if (probe_opl3sa_wss(&cfg) == 0)
299 		return -ENODEV;
300 
301 	found_mpu=probe_opl3sa_mpu(&cfg_mpu);
302 
303 	attach_opl3sa_wss(&cfg);
304 	return 0;
305 }
306 
cleanup_opl3sa(void)307 static void __exit cleanup_opl3sa(void)
308 {
309 	if(found_mpu)
310 		unload_opl3sa_mpu(&cfg_mpu);
311 	unload_opl3sa_wss(&cfg);
312 }
313 
314 module_init(init_opl3sa);
315 module_exit(cleanup_opl3sa);
316 
317 #ifndef MODULE
setup_opl3sa(char * str)318 static int __init setup_opl3sa(char *str)
319 {
320 	/* io, irq, dma, dma2, mpu_io, mpu_irq */
321 	int ints[7];
322 
323 	str = get_options(str, ARRAY_SIZE(ints), ints);
324 
325 	io	= ints[1];
326 	irq	= ints[2];
327 	dma	= ints[3];
328 	dma2	= ints[4];
329 	mpu_io	= ints[5];
330 	mpu_irq	= ints[6];
331 
332 	return 1;
333 }
334 
335 __setup("opl3sa=", setup_opl3sa);
336 #endif
337 MODULE_LICENSE("GPL");
338