xref: /netbsd-src/sys/dev/isa/cms.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /* $NetBSD: cms.c,v 1.2 2001/09/26 21:49:09 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/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/select.h>
41 
42 #include <machine/bus.h>
43 
44 #include <sys/audioio.h>
45 #include <dev/audio_if.h>
46 #include <dev/audiovar.h>
47 
48 #include <sys/midiio.h>
49 #include <dev/midi_if.h>
50 #include <dev/midivar.h>
51 #include <dev/midisynvar.h>
52 
53 #include <dev/isa/isareg.h>
54 #include <dev/isa/isavar.h>
55 #include <dev/isa/cmsreg.h>
56 
57 #ifdef AUDIO_DEBUG
58 #define DPRINTF(x)	if (cmsdebug) printf x
59 int	cmsdebug = 0;
60 #else
61 #define DPRINTF(x)
62 #endif
63 
64 struct cms_softc {
65 	struct midi_softc sc_mididev;
66 
67 	bus_space_tag_t sc_iot;
68 	bus_space_handle_t sc_ioh;
69 
70 	/* shadow registers for each chip */
71 	u_int8_t sc_shadowregs[32*2];
72 	midisyn sc_midisyn;
73 };
74 
75 int	cms_probe __P((struct device *, struct cfdata *, void *));
76 void	cms_attach __P((struct device *, struct device *, void *));
77 
78 struct cfattach cms_ca = {
79 	sizeof(struct cms_softc), cms_probe, cms_attach,
80 };
81 
82 int	cms_open __P((midisyn *, int));
83 void	cms_close __P((midisyn *));
84 void	cms_on __P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
85 void	cms_off	__P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
86 
87 struct midisyn_methods midi_cms_hw = {
88 	cms_open,		/* open */
89 	cms_close,		/* close */
90 	0,			/* ioctl */
91 	0,			/* allocv */
92 	cms_on,			/* noteon */
93 	cms_off,		/* noteoff */
94 	0,			/* keypres */
95 	0,			/* ctlchg */
96 	0,			/* pgmchg */
97 	0,			/* chnpres */
98 	0,			/* pitchb */
99 	0,			/* sysex */
100 };
101 
102 static void cms_reset __P((struct cms_softc *));
103 
104 static char cms_note_table[] = {
105 	/* A  */ 3,
106 	/* A# */ 31,
107 	/* B  */ 58,
108 	/* C  */ 83,
109 	/* C# */ 107,
110 	/* D  */ 130,
111 	/* D# */ 151,
112 	/* E  */ 172,
113 	/* F  */ 191,
114 	/* F# */ 209,
115 	/* G  */ 226,
116 	/* G# */ 242,
117 };
118 
119 #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
120 #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
121 
122 int
123 cms_probe(parent, match, aux)
124 	struct device *parent;
125 	struct cfdata *match;
126 	void *aux;
127 {
128 	struct isa_attach_args *ia = aux;
129 	bus_space_tag_t iot;
130 	bus_space_handle_t ioh;
131 	int found = 0;
132 	int i;
133 
134 	DPRINTF(("cms_probe():\n"));
135 
136 	iot = ia->ia_iot;
137 
138 	if (ia->ia_iobase == IOBASEUNK)
139 		return 0;
140 
141 	if (bus_space_map(iot, ia->ia_iobase, CMS_IOSIZE, 0, &ioh))
142 		return 0;
143 
144 	bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
145 	if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
146 		goto out;
147 
148 	for (i = 0; i < 8; i++) {
149 		if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
150 			goto out;
151 	}
152 	found = 1;
153 
154 	ia->ia_iosize = CMS_IOSIZE;
155 
156 out:
157 	bus_space_unmap(iot, ioh, CMS_IOSIZE);
158 
159 	return found;
160 }
161 
162 
163 void
164 cms_attach(parent, self, aux)
165 	struct device *parent, *self;
166 	void *aux;
167 {
168 	struct cms_softc *sc = (struct cms_softc *)self;
169 	struct isa_attach_args *ia = aux;
170 	bus_space_tag_t iot;
171 	bus_space_handle_t ioh;
172 	midisyn *ms;
173 	struct audio_attach_args arg;
174 
175 	printf("\n");
176 
177 	DPRINTF(("cms_attach():\n"));
178 
179 	iot = ia->ia_iot;
180 
181 	if (bus_space_map(iot, ia->ia_iobase, CMS_IOSIZE, 0, &ioh)) {
182 		printf(": can't map i/o space\n");
183 		return;
184 	}
185 
186 	sc->sc_iot = iot;
187 	sc->sc_ioh = ioh;
188 
189 	/* now let's reset the chips */
190 	cms_reset(sc);
191 
192 	ms = &sc->sc_midisyn;
193 	ms->mets = &midi_cms_hw;
194 	strcpy(ms->name, "Creative Music System");
195 	ms->nvoice = CMS_NVOICES;
196 	ms->flags = MS_DOALLOC;
197 	ms->data = sc;
198 
199 	/* use the synthesiser */
200 	midisyn_attach(&sc->sc_mididev, ms);
201 
202 	/* now attach the midi device to the synthesiser */
203 	arg.type = AUDIODEV_TYPE_MIDI;
204 	arg.hwif = sc->sc_mididev.hw_if;
205 	arg.hdl = sc->sc_mididev.hw_hdl;
206 	config_found((struct device *)&sc->sc_mididev, &arg, 0);
207 }
208 
209 
210 int
211 cms_open(ms,flag)
212 	midisyn *ms;
213 	int flag;
214 {
215 	struct cms_softc *sc = (struct cms_softc *)ms->data;
216 
217 	cms_reset(sc);
218 
219 	return 0;
220 }
221 
222 void
223 cms_close(ms)
224 	midisyn *ms;
225 {
226 	struct cms_softc *sc = (struct cms_softc *)ms->data;
227 
228 	cms_reset(sc);
229 }
230 
231 void
232 cms_on(ms, chan, note, vel)
233 	midisyn *ms;
234 	u_int32_t chan;
235 	u_int32_t note;
236 	u_int32_t vel;
237 {
238 	struct cms_softc *sc = (struct cms_softc *)ms->data;
239 	int chip = CHAN_TO_CHIP(chan);
240 	int voice = CHAN_TO_VOICE(chan);
241 	u_int8_t octave;
242 	u_int8_t count;
243 	u_int8_t reg;
244 	u_int8_t vol;
245 
246 	if (note < CMS_FIRST_NOTE)
247 		return;
248 
249 	octave = NOTE_TO_OCTAVE(note);
250 	count = NOTE_TO_COUNT(note);
251 
252 	DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
253 		chip, voice, octave, count, OCTAVE_OFFSET(voice),
254 		OCTAVE_SHIFT(voice)));
255 
256 	/* write the count */
257 	CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
258 
259 	/* select the octave */
260 	reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
261 	reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
262 	reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
263 	CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
264 
265 	/* set the volume */
266 	vol = (vel>>3)&0x0f;
267 	CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
268 
269 	/* enable the voice */
270 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
271 	reg |= (1<<voice);
272 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
273 }
274 
275 void
276 cms_off(ms, chan, note, vel)
277 	midisyn *ms;
278 	u_int32_t chan;
279 	u_int32_t note;
280 	u_int32_t vel;
281 {
282 	struct cms_softc *sc = (struct cms_softc *)ms->data;
283 	int chip = CHAN_TO_CHIP(chan);
284 	int voice = CHAN_TO_VOICE(chan);
285 	u_int8_t reg;
286 
287 	if (note < CMS_FIRST_NOTE)
288 		return;
289 
290 	/* disable the channel */
291 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
292 	reg &= ~(1<<voice);
293 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
294 }
295 
296 static void
297 cms_reset(sc)
298 	struct cms_softc *sc;
299 {
300 	int i;
301 
302 	DPRINTF(("cms_reset():\n"));
303 
304 	for (i = 0; i < 6; i++) {
305 		CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
306 		CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
307 
308 		CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
309 		CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
310 	}
311 
312 	for (i = 0; i < 3; i++) {
313 		CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
314 		CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
315 	}
316 
317 	CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
318 	CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
319 
320 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
321 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
322 
323 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
324 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
325 
326 	/*
327 	 * These registers don't appear to be useful, but must be
328 	 * cleared otherwise certain voices don't work properly
329 	 */
330 	CMS_WRITE(sc, 0, 0x18, 0x00);
331 	CMS_WRITE(sc, 1, 0x18, 0x00);
332 	CMS_WRITE(sc, 0, 0x19, 0x00);
333 	CMS_WRITE(sc, 1, 0x19, 0x00);
334 
335 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
336 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
337 
338 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
339 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
340 }
341