Home | History | Annotate | Line # | Download | only in isa
cms.c revision 1.1
      1 /* $NetBSD: cms.c,v 1.1 2000/05/01 22:48:33 augustss 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 "cms.h"
     37 #if NCMS > 0
     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_iobase == IOBASEUNK)
    142 		return 0;
    143 
    144 	if (bus_space_map(iot, ia->ia_iobase, CMS_IOSIZE, 0, &ioh))
    145 		return 0;
    146 
    147 	bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
    148 	if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
    149 		goto out;
    150 
    151 	for (i = 0; i < 8; i++) {
    152 		if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
    153 			goto out;
    154 	}
    155 	found = 1;
    156 
    157 	ia->ia_iosize = CMS_IOSIZE;
    158 
    159 out:
    160 	bus_space_unmap(iot, ioh, CMS_IOSIZE);
    161 
    162 	return found;
    163 }
    164 
    165 
    166 void
    167 cms_attach(parent, self, aux)
    168 	struct device *parent, *self;
    169 	void *aux;
    170 {
    171 	struct cms_softc *sc = (struct cms_softc *)self;
    172 	struct isa_attach_args *ia = aux;
    173 	bus_space_tag_t iot;
    174 	bus_space_handle_t ioh;
    175 	midisyn *ms;
    176 	struct audio_attach_args arg;
    177 
    178 	printf("\n");
    179 
    180 	DPRINTF(("cms_attach():\n"));
    181 
    182 	iot = ia->ia_iot;
    183 
    184 	if (bus_space_map(iot, ia->ia_iobase, CMS_IOSIZE, 0, &ioh)) {
    185 		printf(": can't map i/o space\n");
    186 		return;
    187 	}
    188 
    189 	sc->sc_iot = iot;
    190 	sc->sc_ioh = ioh;
    191 
    192 	/* now let's reset the chips */
    193 	cms_reset(sc);
    194 
    195 	ms = &sc->sc_midisyn;
    196 	ms->mets = &midi_cms_hw;
    197 	strcpy(ms->name, "Creative Music System");
    198 	ms->nvoice = CMS_NVOICES;
    199 	ms->flags = MS_DOALLOC;
    200 	ms->data = sc;
    201 
    202 	/* use the synthesiser */
    203 	midisyn_attach(&sc->sc_mididev, ms);
    204 
    205 	/* now attach the midi device to the synthesiser */
    206 	arg.type = AUDIODEV_TYPE_MIDI;
    207 	arg.hwif = sc->sc_mididev.hw_if;
    208 	arg.hdl = sc->sc_mididev.hw_hdl;
    209 	config_found((struct device *)&sc->sc_mididev, &arg, 0);
    210 }
    211 
    212 
    213 int
    214 cms_open(ms,flag)
    215 	midisyn *ms;
    216 	int flag;
    217 {
    218 	struct cms_softc *sc = (struct cms_softc *)ms->data;
    219 
    220 	cms_reset(sc);
    221 
    222 	return 0;
    223 }
    224 
    225 void
    226 cms_close(ms)
    227 	midisyn *ms;
    228 {
    229 	struct cms_softc *sc = (struct cms_softc *)ms->data;
    230 
    231 	cms_reset(sc);
    232 }
    233 
    234 void
    235 cms_on(ms, chan, note, vel)
    236 	midisyn *ms;
    237 	u_int32_t chan;
    238 	u_int32_t note;
    239 	u_int32_t vel;
    240 {
    241 	struct cms_softc *sc = (struct cms_softc *)ms->data;
    242 	int chip = CHAN_TO_CHIP(chan);
    243 	int voice = CHAN_TO_VOICE(chan);
    244 	u_int8_t octave;
    245 	u_int8_t count;
    246 	u_int8_t reg;
    247 	u_int8_t vol;
    248 
    249 	if (note < CMS_FIRST_NOTE)
    250 		return;
    251 
    252 	octave = NOTE_TO_OCTAVE(note);
    253 	count = NOTE_TO_COUNT(note);
    254 
    255 	DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
    256 		chip, voice, octave, count, OCTAVE_OFFSET(voice),
    257 		OCTAVE_SHIFT(voice)));
    258 
    259 	/* write the count */
    260 	CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
    261 
    262 	/* select the octave */
    263 	reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
    264 	reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
    265 	reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
    266 	CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
    267 
    268 	/* set the volume */
    269 	vol = (vel>>3)&0x0f;
    270 	CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
    271 
    272 	/* enable the voice */
    273 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
    274 	reg |= (1<<voice);
    275 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
    276 }
    277 
    278 void
    279 cms_off(ms, chan, note, vel)
    280 	midisyn *ms;
    281 	u_int32_t chan;
    282 	u_int32_t note;
    283 	u_int32_t vel;
    284 {
    285 	struct cms_softc *sc = (struct cms_softc *)ms->data;
    286 	int chip = CHAN_TO_CHIP(chan);
    287 	int voice = CHAN_TO_VOICE(chan);
    288 	u_int8_t reg;
    289 
    290 	if (note < CMS_FIRST_NOTE)
    291 		return;
    292 
    293 	/* disable the channel */
    294 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
    295 	reg &= ~(1<<voice);
    296 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
    297 }
    298 
    299 static void
    300 cms_reset(sc)
    301 	struct cms_softc *sc;
    302 {
    303 	int i;
    304 
    305 	DPRINTF(("cms_reset():\n"));
    306 
    307 	for (i = 0; i < 6; i++) {
    308 		CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
    309 		CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
    310 
    311 		CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
    312 		CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
    313 	}
    314 
    315 	for (i = 0; i < 3; i++) {
    316 		CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
    317 		CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
    318 	}
    319 
    320 	CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
    321 	CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
    322 
    323 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
    324 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
    325 
    326 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
    327 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
    328 
    329 	/*
    330 	 * These registers don't appear to be useful, but must be
    331 	 * cleared otherwise certain voices don't work properly
    332 	 */
    333 	CMS_WRITE(sc, 0, 0x18, 0x00);
    334 	CMS_WRITE(sc, 1, 0x18, 0x00);
    335 	CMS_WRITE(sc, 0, 0x19, 0x00);
    336 	CMS_WRITE(sc, 1, 0x19, 0x00);
    337 
    338 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
    339 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
    340 
    341 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
    342 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
    343 }
    344 
    345 #endif
    346