cms.c revision 1.21 1 /* $NetBSD: cms.c,v 1.21 2012/04/09 10:18:16 plunky 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cms.c,v 1.21 2012/04/09 10:18:16 plunky Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/select.h>
37
38 #include <sys/bus.h>
39
40 #include <sys/audioio.h>
41 #include <dev/audio_if.h>
42 #include <dev/audiovar.h>
43
44 #include <sys/midiio.h>
45 #include <dev/midi_if.h>
46 #include <dev/midivar.h>
47 #include <dev/midisynvar.h>
48
49 #include <dev/isa/isareg.h>
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/cmsreg.h>
52
53 #ifdef AUDIO_DEBUG
54 #define DPRINTF(x) if (cmsdebug) printf x
55 int cmsdebug = 0;
56 #else
57 #define DPRINTF(x)
58 #endif
59
60 struct cms_softc {
61 kmutex_t sc_lock;
62
63 bus_space_tag_t sc_iot;
64 bus_space_handle_t sc_ioh;
65
66 /* shadow registers for each chip */
67 u_int8_t sc_shadowregs[32*2];
68 midisyn sc_midisyn;
69 };
70
71 int cms_probe(device_t, cfdata_t, void *);
72 void cms_attach(device_t, device_t, void *);
73
74 CFATTACH_DECL_NEW(cms, sizeof(struct cms_softc),
75 cms_probe, cms_attach, NULL, NULL);
76
77 int cms_open(midisyn *, int);
78 void cms_close(midisyn *);
79 void cms_on(midisyn *, uint_fast16_t, midipitch_t, int16_t);
80 void cms_off(midisyn *, uint_fast16_t, uint_fast8_t);
81
82 struct midisyn_methods midi_cms_hw = {
83 .open = cms_open,
84 .close = cms_close,
85 .attackv = cms_on,
86 .releasev = cms_off,
87 };
88
89 static void cms_reset(struct cms_softc *);
90
91 static char cms_note_table[] = {
92 /* A */ 3,
93 /* A# */ 31,
94 /* B */ 58,
95 /* C */ 83,
96 /* C# */ 107,
97 /* D */ 130,
98 /* D# */ 151,
99 /* E */ 172,
100 /* F */ 191,
101 /* F# */ 209,
102 /* G */ 226,
103 /* G# */ 242,
104 };
105
106 #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
107 #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
108
109 int
110 cms_probe(device_t parent, cfdata_t match, void *aux)
111 {
112 struct isa_attach_args *ia = aux;
113 bus_space_tag_t iot;
114 bus_space_handle_t ioh;
115 int found = 0;
116 int i;
117
118 DPRINTF(("cms_probe():\n"));
119
120 iot = ia->ia_iot;
121
122 if (ia->ia_nio < 1)
123 return 0;
124
125 if (ISA_DIRECT_CONFIG(ia))
126 return 0;
127
128 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
129 return 0;
130
131 if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh))
132 return 0;
133
134 bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
135 if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
136 goto out;
137
138 for (i = 0; i < 8; i++) {
139 if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
140 goto out;
141 }
142 found = 1;
143
144 ia->ia_nio = 1;
145 ia->ia_io[0].ir_size = CMS_IOSIZE;
146
147 ia->ia_niomem = 0;
148 ia->ia_nirq = 0;
149 ia->ia_ndrq = 0;
150
151 out:
152 bus_space_unmap(iot, ioh, CMS_IOSIZE);
153
154 return found;
155 }
156
157
158 void
159 cms_attach(device_t parent, device_t self, void *aux)
160 {
161 struct cms_softc *sc = device_private(self);
162 struct isa_attach_args *ia = aux;
163 bus_space_tag_t iot;
164 bus_space_handle_t ioh;
165 midisyn *ms;
166
167 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_AUDIO);
168
169 aprint_normal("\n");
170
171 DPRINTF(("cms_attach():\n"));
172
173 iot = ia->ia_iot;
174
175 if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh)) {
176 aprint_error_dev(self, "can't map i/o space\n");
177 return;
178 }
179
180 sc->sc_iot = iot;
181 sc->sc_ioh = ioh;
182
183 /* now let's reset the chips */
184 cms_reset(sc);
185
186 /* init the synthesiser */
187 ms = &sc->sc_midisyn;
188 ms->mets = &midi_cms_hw;
189 strcpy(ms->name, "Creative Music System");
190 ms->nvoice = CMS_NVOICES;
191 ms->data = sc;
192 ms->lock = &sc->sc_lock;
193 midisyn_init(ms);
194
195 /* and attach the midi device with the synthesiser */
196 midi_attach_mi(&midisyn_hw_if, ms, self);
197 }
198
199
200 int
201 cms_open(midisyn *ms, int flag)
202 {
203 struct cms_softc *sc = (struct cms_softc *)ms->data;
204
205 cms_reset(sc);
206
207 return 0;
208 }
209
210 void
211 cms_close(midisyn *ms)
212 {
213 struct cms_softc *sc = (struct cms_softc *)ms->data;
214
215 cms_reset(sc);
216 }
217
218 void
219 cms_on(midisyn *ms, uint_fast16_t vidx, midipitch_t mp, int16_t level_cB)
220 {
221 struct cms_softc *sc = (struct cms_softc *)ms->data;
222 int chip = CHAN_TO_CHIP(vidx);
223 int voice = CHAN_TO_VOICE(vidx);
224 uint32_t note;
225 u_int8_t octave;
226 u_int8_t count;
227 u_int8_t reg;
228 u_int8_t vol;
229
230 /*
231 * The next line is a regrettable hack, because it drops all pitch
232 * adjustment midisyn has supplied in miditune, so this synth will
233 * not respond to tuning, pitchbend, etc. It seems it ought to be
234 * possible to DTRT if the formula that generated the cms_note_table
235 * can be found, but I've had no luck, and a plot of the numbers in
236 * the table clearly curves the wrong way to be derived any obvious
237 * way from the equal tempered scale. (Or maybe the table's wrong?
238 * Has this device been playing flat up the scale? I don't have
239 * access to one to try.)
240 */
241 note = MIDIPITCH_TO_KEY(mp);
242
243 if (note < CMS_FIRST_NOTE)
244 return;
245
246 octave = NOTE_TO_OCTAVE(note);
247 count = NOTE_TO_COUNT(note);
248
249 DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
250 chip, voice, octave, count, OCTAVE_OFFSET(voice),
251 OCTAVE_SHIFT(voice)));
252
253 /* write the count */
254 CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
255
256 /* select the octave */
257 reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
258 reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
259 reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
260 CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
261
262 /* set the volume */
263 /* this may be the wrong curve but will do something. no docs! */
264 vol = 15 + (level_cB > -75) ? level_cB/5 : -15;
265 CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
266
267 /* enable the voice */
268 reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
269 reg |= (1<<voice);
270 CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
271 }
272
273 void
274 cms_off(midisyn *ms, uint_fast16_t vidx, uint_fast8_t vel)
275 {
276 struct cms_softc *sc = (struct cms_softc *)ms->data;
277 int chip = CHAN_TO_CHIP(vidx);
278 int voice = CHAN_TO_VOICE(vidx);
279 u_int8_t reg;
280
281 /* disable the channel */
282 reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
283 reg &= ~(1<<voice);
284 CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
285 }
286
287 static void
288 cms_reset(struct cms_softc *sc)
289 {
290 int i;
291
292 DPRINTF(("cms_reset():\n"));
293
294 for (i = 0; i < 6; i++) {
295 CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
296 CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
297
298 CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
299 CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
300 }
301
302 for (i = 0; i < 3; i++) {
303 CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
304 CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
305 }
306
307 CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
308 CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
309
310 CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
311 CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
312
313 CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
314 CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
315
316 /*
317 * These registers don't appear to be useful, but must be
318 * cleared otherwise certain voices don't work properly
319 */
320 CMS_WRITE(sc, 0, 0x18, 0x00);
321 CMS_WRITE(sc, 1, 0x18, 0x00);
322 CMS_WRITE(sc, 0, 0x19, 0x00);
323 CMS_WRITE(sc, 1, 0x19, 0x00);
324
325 CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
326 CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
327
328 CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
329 CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
330 }
331