xref: /netbsd/sys/dev/isa/cms.c (revision bf9ec67e)
1 /* $NetBSD: cms.c,v 1.4 2002/01/07 21:47:04 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: cms.c,v 1.4 2002/01/07 21:47:04 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/select.h>
44 
45 #include <machine/bus.h>
46 
47 #include <sys/audioio.h>
48 #include <dev/audio_if.h>
49 #include <dev/audiovar.h>
50 
51 #include <sys/midiio.h>
52 #include <dev/midi_if.h>
53 #include <dev/midivar.h>
54 #include <dev/midisynvar.h>
55 
56 #include <dev/isa/isareg.h>
57 #include <dev/isa/isavar.h>
58 #include <dev/isa/cmsreg.h>
59 
60 #ifdef AUDIO_DEBUG
61 #define DPRINTF(x)	if (cmsdebug) printf x
62 int	cmsdebug = 0;
63 #else
64 #define DPRINTF(x)
65 #endif
66 
67 struct cms_softc {
68 	struct midi_softc sc_mididev;
69 
70 	bus_space_tag_t sc_iot;
71 	bus_space_handle_t sc_ioh;
72 
73 	/* shadow registers for each chip */
74 	u_int8_t sc_shadowregs[32*2];
75 	midisyn sc_midisyn;
76 };
77 
78 int	cms_probe __P((struct device *, struct cfdata *, void *));
79 void	cms_attach __P((struct device *, struct device *, void *));
80 
81 struct cfattach cms_ca = {
82 	sizeof(struct cms_softc), cms_probe, cms_attach,
83 };
84 
85 int	cms_open __P((midisyn *, int));
86 void	cms_close __P((midisyn *));
87 void	cms_on __P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
88 void	cms_off	__P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
89 
90 struct midisyn_methods midi_cms_hw = {
91 	cms_open,		/* open */
92 	cms_close,		/* close */
93 	0,			/* ioctl */
94 	0,			/* allocv */
95 	cms_on,			/* noteon */
96 	cms_off,		/* noteoff */
97 	0,			/* keypres */
98 	0,			/* ctlchg */
99 	0,			/* pgmchg */
100 	0,			/* chnpres */
101 	0,			/* pitchb */
102 	0,			/* sysex */
103 };
104 
105 static void cms_reset __P((struct cms_softc *));
106 
107 static char cms_note_table[] = {
108 	/* A  */ 3,
109 	/* A# */ 31,
110 	/* B  */ 58,
111 	/* C  */ 83,
112 	/* C# */ 107,
113 	/* D  */ 130,
114 	/* D# */ 151,
115 	/* E  */ 172,
116 	/* F  */ 191,
117 	/* F# */ 209,
118 	/* G  */ 226,
119 	/* G# */ 242,
120 };
121 
122 #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
123 #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
124 
125 int
126 cms_probe(parent, match, aux)
127 	struct device *parent;
128 	struct cfdata *match;
129 	void *aux;
130 {
131 	struct isa_attach_args *ia = aux;
132 	bus_space_tag_t iot;
133 	bus_space_handle_t ioh;
134 	int found = 0;
135 	int i;
136 
137 	DPRINTF(("cms_probe():\n"));
138 
139 	iot = ia->ia_iot;
140 
141 	if (ia->ia_nio < 1)
142 		return 0;
143 
144 	if (ISA_DIRECT_CONFIG(ia))
145 		return 0;
146 
147 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
148 		return 0;
149 
150 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh))
151 		return 0;
152 
153 	bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
154 	if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
155 		goto out;
156 
157 	for (i = 0; i < 8; i++) {
158 		if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
159 			goto out;
160 	}
161 	found = 1;
162 
163 	ia->ia_nio = 1;
164 	ia->ia_io[0].ir_size = CMS_IOSIZE;
165 
166 	ia->ia_niomem = 0;
167 	ia->ia_nirq = 0;
168 	ia->ia_ndrq = 0;
169 
170 out:
171 	bus_space_unmap(iot, ioh, CMS_IOSIZE);
172 
173 	return found;
174 }
175 
176 
177 void
178 cms_attach(parent, self, aux)
179 	struct device *parent, *self;
180 	void *aux;
181 {
182 	struct cms_softc *sc = (struct cms_softc *)self;
183 	struct isa_attach_args *ia = aux;
184 	bus_space_tag_t iot;
185 	bus_space_handle_t ioh;
186 	midisyn *ms;
187 	struct audio_attach_args arg;
188 
189 	printf("\n");
190 
191 	DPRINTF(("cms_attach():\n"));
192 
193 	iot = ia->ia_iot;
194 
195 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh)) {
196 		printf(": can't map i/o space\n");
197 		return;
198 	}
199 
200 	sc->sc_iot = iot;
201 	sc->sc_ioh = ioh;
202 
203 	/* now let's reset the chips */
204 	cms_reset(sc);
205 
206 	ms = &sc->sc_midisyn;
207 	ms->mets = &midi_cms_hw;
208 	strcpy(ms->name, "Creative Music System");
209 	ms->nvoice = CMS_NVOICES;
210 	ms->flags = MS_DOALLOC;
211 	ms->data = sc;
212 
213 	/* use the synthesiser */
214 	midisyn_attach(&sc->sc_mididev, ms);
215 
216 	/* now attach the midi device to the synthesiser */
217 	arg.type = AUDIODEV_TYPE_MIDI;
218 	arg.hwif = sc->sc_mididev.hw_if;
219 	arg.hdl = sc->sc_mididev.hw_hdl;
220 	config_found((struct device *)&sc->sc_mididev, &arg, 0);
221 }
222 
223 
224 int
225 cms_open(ms,flag)
226 	midisyn *ms;
227 	int flag;
228 {
229 	struct cms_softc *sc = (struct cms_softc *)ms->data;
230 
231 	cms_reset(sc);
232 
233 	return 0;
234 }
235 
236 void
237 cms_close(ms)
238 	midisyn *ms;
239 {
240 	struct cms_softc *sc = (struct cms_softc *)ms->data;
241 
242 	cms_reset(sc);
243 }
244 
245 void
246 cms_on(ms, chan, note, vel)
247 	midisyn *ms;
248 	u_int32_t chan;
249 	u_int32_t note;
250 	u_int32_t vel;
251 {
252 	struct cms_softc *sc = (struct cms_softc *)ms->data;
253 	int chip = CHAN_TO_CHIP(chan);
254 	int voice = CHAN_TO_VOICE(chan);
255 	u_int8_t octave;
256 	u_int8_t count;
257 	u_int8_t reg;
258 	u_int8_t vol;
259 
260 	if (note < CMS_FIRST_NOTE)
261 		return;
262 
263 	octave = NOTE_TO_OCTAVE(note);
264 	count = NOTE_TO_COUNT(note);
265 
266 	DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
267 		chip, voice, octave, count, OCTAVE_OFFSET(voice),
268 		OCTAVE_SHIFT(voice)));
269 
270 	/* write the count */
271 	CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
272 
273 	/* select the octave */
274 	reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
275 	reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
276 	reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
277 	CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
278 
279 	/* set the volume */
280 	vol = (vel>>3)&0x0f;
281 	CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
282 
283 	/* enable the voice */
284 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
285 	reg |= (1<<voice);
286 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
287 }
288 
289 void
290 cms_off(ms, chan, note, vel)
291 	midisyn *ms;
292 	u_int32_t chan;
293 	u_int32_t note;
294 	u_int32_t vel;
295 {
296 	struct cms_softc *sc = (struct cms_softc *)ms->data;
297 	int chip = CHAN_TO_CHIP(chan);
298 	int voice = CHAN_TO_VOICE(chan);
299 	u_int8_t reg;
300 
301 	if (note < CMS_FIRST_NOTE)
302 		return;
303 
304 	/* disable the channel */
305 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
306 	reg &= ~(1<<voice);
307 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
308 }
309 
310 static void
311 cms_reset(sc)
312 	struct cms_softc *sc;
313 {
314 	int i;
315 
316 	DPRINTF(("cms_reset():\n"));
317 
318 	for (i = 0; i < 6; i++) {
319 		CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
320 		CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
321 
322 		CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
323 		CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
324 	}
325 
326 	for (i = 0; i < 3; i++) {
327 		CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
328 		CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
329 	}
330 
331 	CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
332 	CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
333 
334 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
335 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
336 
337 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
338 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
339 
340 	/*
341 	 * These registers don't appear to be useful, but must be
342 	 * cleared otherwise certain voices don't work properly
343 	 */
344 	CMS_WRITE(sc, 0, 0x18, 0x00);
345 	CMS_WRITE(sc, 1, 0x18, 0x00);
346 	CMS_WRITE(sc, 0, 0x19, 0x00);
347 	CMS_WRITE(sc, 1, 0x19, 0x00);
348 
349 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
350 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
351 
352 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
353 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
354 }
355