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