cms.c revision 1.11.14.1 1 /* $NetBSD: cms.c,v 1.11.14.1 2006/06/07 01:23:09 chap 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.11.14.1 2006/06/07 01:23:09 chap 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(struct device *, struct cfdata *, void *);
79 void cms_attach(struct device *, struct device *, void *);
80
81 CFATTACH_DECL(cms, sizeof(struct cms_softc),
82 cms_probe, cms_attach, NULL, NULL);
83
84 int cms_open(midisyn *, int);
85 void cms_close(midisyn *);
86 void cms_on(midisyn *, u_int32_t, u_int32_t, u_int32_t);
87 void cms_off(midisyn *, u_int32_t, u_int32_t, u_int32_t);
88
89 struct midisyn_methods midi_cms_hw = {
90 .open = cms_open,
91 .close = cms_close,
92 .noteon = cms_on,
93 .noteoff = cms_off,
94 };
95
96 static void cms_reset(struct cms_softc *);
97
98 static char cms_note_table[] = {
99 /* A */ 3,
100 /* A# */ 31,
101 /* B */ 58,
102 /* C */ 83,
103 /* C# */ 107,
104 /* D */ 130,
105 /* D# */ 151,
106 /* E */ 172,
107 /* F */ 191,
108 /* F# */ 209,
109 /* G */ 226,
110 /* G# */ 242,
111 };
112
113 #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
114 #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
115
116 int
117 cms_probe(parent, match, aux)
118 struct device *parent;
119 struct cfdata *match;
120 void *aux;
121 {
122 struct isa_attach_args *ia = aux;
123 bus_space_tag_t iot;
124 bus_space_handle_t ioh;
125 int found = 0;
126 int i;
127
128 DPRINTF(("cms_probe():\n"));
129
130 iot = ia->ia_iot;
131
132 if (ia->ia_nio < 1)
133 return 0;
134
135 if (ISA_DIRECT_CONFIG(ia))
136 return 0;
137
138 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
139 return 0;
140
141 if (bus_space_map(iot, ia->ia_io[0].ir_addr, 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_nio = 1;
155 ia->ia_io[0].ir_size = CMS_IOSIZE;
156
157 ia->ia_niomem = 0;
158 ia->ia_nirq = 0;
159 ia->ia_ndrq = 0;
160
161 out:
162 bus_space_unmap(iot, ioh, CMS_IOSIZE);
163
164 return found;
165 }
166
167
168 void
169 cms_attach(parent, self, aux)
170 struct device *parent, *self;
171 void *aux;
172 {
173 struct cms_softc *sc = (struct cms_softc *)self;
174 struct isa_attach_args *ia = aux;
175 bus_space_tag_t iot;
176 bus_space_handle_t ioh;
177 midisyn *ms;
178 struct audio_attach_args arg;
179
180 printf("\n");
181
182 DPRINTF(("cms_attach():\n"));
183
184 iot = ia->ia_iot;
185
186 if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh)) {
187 printf(": can't map i/o space\n");
188 return;
189 }
190
191 sc->sc_iot = iot;
192 sc->sc_ioh = ioh;
193
194 /* now let's reset the chips */
195 cms_reset(sc);
196
197 ms = &sc->sc_midisyn;
198 ms->mets = &midi_cms_hw;
199 strcpy(ms->name, "Creative Music System");
200 ms->nvoice = CMS_NVOICES;
201 ms->flags = MS_DOALLOC;
202 ms->data = sc;
203
204 /* use the synthesiser */
205 midisyn_attach(&sc->sc_mididev, ms);
206
207 /* now attach the midi device to the synthesiser */
208 arg.type = AUDIODEV_TYPE_MIDI;
209 arg.hwif = sc->sc_mididev.hw_if;
210 arg.hdl = sc->sc_mididev.hw_hdl;
211 config_found((struct device *)&sc->sc_mididev, &arg, 0);
212 }
213
214
215 int
216 cms_open(ms,flag)
217 midisyn *ms;
218 int flag;
219 {
220 struct cms_softc *sc = (struct cms_softc *)ms->data;
221
222 cms_reset(sc);
223
224 return 0;
225 }
226
227 void
228 cms_close(ms)
229 midisyn *ms;
230 {
231 struct cms_softc *sc = (struct cms_softc *)ms->data;
232
233 cms_reset(sc);
234 }
235
236 void
237 cms_on(ms, chan, note, vel)
238 midisyn *ms;
239 u_int32_t chan;
240 u_int32_t note;
241 u_int32_t vel;
242 {
243 struct cms_softc *sc = (struct cms_softc *)ms->data;
244 int chip = CHAN_TO_CHIP(chan);
245 int voice = CHAN_TO_VOICE(chan);
246 u_int8_t octave;
247 u_int8_t count;
248 u_int8_t reg;
249 u_int8_t vol;
250
251 if (note < CMS_FIRST_NOTE)
252 return;
253
254 octave = NOTE_TO_OCTAVE(note);
255 count = NOTE_TO_COUNT(note);
256
257 DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
258 chip, voice, octave, count, OCTAVE_OFFSET(voice),
259 OCTAVE_SHIFT(voice)));
260
261 /* write the count */
262 CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
263
264 /* select the octave */
265 reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
266 reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
267 reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
268 CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
269
270 /* set the volume */
271 vol = (vel>>3)&0x0f;
272 CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
273
274 /* enable the voice */
275 reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
276 reg |= (1<<voice);
277 CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
278 }
279
280 void
281 cms_off(ms, chan, note, vel)
282 midisyn *ms;
283 u_int32_t chan;
284 u_int32_t note;
285 u_int32_t vel;
286 {
287 struct cms_softc *sc = (struct cms_softc *)ms->data;
288 int chip = CHAN_TO_CHIP(chan);
289 int voice = CHAN_TO_VOICE(chan);
290 u_int8_t reg;
291
292 if (note < CMS_FIRST_NOTE)
293 return;
294
295 /* disable the channel */
296 reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
297 reg &= ~(1<<voice);
298 CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
299 }
300
301 static void
302 cms_reset(sc)
303 struct cms_softc *sc;
304 {
305 int i;
306
307 DPRINTF(("cms_reset():\n"));
308
309 for (i = 0; i < 6; i++) {
310 CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
311 CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
312
313 CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
314 CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
315 }
316
317 for (i = 0; i < 3; i++) {
318 CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
319 CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
320 }
321
322 CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
323 CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
324
325 CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
326 CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
327
328 CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
329 CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
330
331 /*
332 * These registers don't appear to be useful, but must be
333 * cleared otherwise certain voices don't work properly
334 */
335 CMS_WRITE(sc, 0, 0x18, 0x00);
336 CMS_WRITE(sc, 1, 0x18, 0x00);
337 CMS_WRITE(sc, 0, 0x19, 0x00);
338 CMS_WRITE(sc, 1, 0x19, 0x00);
339
340 CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
341 CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
342
343 CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
344 CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
345 }
346