xref: /netbsd-src/sys/dev/isa/cms.c (revision e622eac459adf11c2e710d7a4de0f05510bbbe61)
1*e622eac4Sisaki /* $NetBSD: cms.c,v 1.23 2019/05/08 13:40:18 isaki Exp $ */
22365a92cSaugustss 
32365a92cSaugustss /*
42365a92cSaugustss  * Copyright (c) 2000 The NetBSD Foundation, Inc.
52365a92cSaugustss  * All rights reserved.
62365a92cSaugustss  *
72365a92cSaugustss  * Redistribution and use in source and binary forms, with or without
82365a92cSaugustss  * modification, are permitted provided that the following conditions
92365a92cSaugustss  * are met:
102365a92cSaugustss  * 1. Redistributions of source code must retain the above copyright
112365a92cSaugustss  *    notice, this list of conditions and the following disclaimer.
122365a92cSaugustss  * 2. Redistributions in binary form must reproduce the above copyright
132365a92cSaugustss  *    notice, this list of conditions and the following disclaimer in the
142365a92cSaugustss  *    documentation and/or other materials provided with the distribution.
152365a92cSaugustss  *
162365a92cSaugustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172365a92cSaugustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182365a92cSaugustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192365a92cSaugustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202365a92cSaugustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212365a92cSaugustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222365a92cSaugustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232365a92cSaugustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242365a92cSaugustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252365a92cSaugustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262365a92cSaugustss  * POSSIBILITY OF SUCH DAMAGE.
272365a92cSaugustss  */
282365a92cSaugustss 
293f7d8d47Slukem #include <sys/cdefs.h>
30*e622eac4Sisaki __KERNEL_RCSID(0, "$NetBSD: cms.c,v 1.23 2019/05/08 13:40:18 isaki Exp $");
313f7d8d47Slukem 
322365a92cSaugustss #include <sys/param.h>
332365a92cSaugustss #include <sys/systm.h>
342365a92cSaugustss #include <sys/kernel.h>
352365a92cSaugustss #include <sys/device.h>
362365a92cSaugustss #include <sys/select.h>
372365a92cSaugustss 
38a2a38285Sad #include <sys/bus.h>
392365a92cSaugustss 
402365a92cSaugustss #include <sys/audioio.h>
41*e622eac4Sisaki #include <dev/audio/audio_if.h>
422365a92cSaugustss 
432365a92cSaugustss #include <sys/midiio.h>
442365a92cSaugustss #include <dev/midi_if.h>
452365a92cSaugustss #include <dev/midivar.h>
462365a92cSaugustss #include <dev/midisynvar.h>
472365a92cSaugustss 
482365a92cSaugustss #include <dev/isa/isareg.h>
492365a92cSaugustss #include <dev/isa/isavar.h>
502365a92cSaugustss #include <dev/isa/cmsreg.h>
512365a92cSaugustss 
522365a92cSaugustss #ifdef AUDIO_DEBUG
532365a92cSaugustss #define DPRINTF(x)	if (cmsdebug) printf x
542365a92cSaugustss int	cmsdebug = 0;
552365a92cSaugustss #else
562365a92cSaugustss #define DPRINTF(x)
572365a92cSaugustss #endif
582365a92cSaugustss 
592365a92cSaugustss struct cms_softc {
60ca6ee1e0Sjakllsch 	kmutex_t sc_lock;
61ca6ee1e0Sjakllsch 
622365a92cSaugustss 	bus_space_tag_t sc_iot;
632365a92cSaugustss 	bus_space_handle_t sc_ioh;
642365a92cSaugustss 
652365a92cSaugustss 	/* shadow registers for each chip */
662365a92cSaugustss 	u_int8_t sc_shadowregs[32*2];
672365a92cSaugustss 	midisyn sc_midisyn;
682365a92cSaugustss };
692365a92cSaugustss 
704dccf0aeScube int	cms_probe(device_t, cfdata_t, void *);
714dccf0aeScube void	cms_attach(device_t, device_t, void *);
722365a92cSaugustss 
734dccf0aeScube CFATTACH_DECL_NEW(cms, sizeof(struct cms_softc),
740dac35b5Sthorpej     cms_probe, cms_attach, NULL, NULL);
752365a92cSaugustss 
7618db93c7Sperry int	cms_open(midisyn *, int);
7718db93c7Sperry void	cms_close(midisyn *);
78710af634Schap void	cms_on(midisyn *, uint_fast16_t, midipitch_t, int16_t);
79710af634Schap void	cms_off(midisyn *, uint_fast16_t, uint_fast8_t);
802365a92cSaugustss 
812365a92cSaugustss struct midisyn_methods midi_cms_hw = {
82710af634Schap 	.open     = cms_open,
83710af634Schap 	.close    = cms_close,
84710af634Schap 	.attackv  = cms_on,
85710af634Schap 	.releasev = cms_off,
862365a92cSaugustss };
872365a92cSaugustss 
8818db93c7Sperry static void cms_reset(struct cms_softc *);
892365a92cSaugustss 
902365a92cSaugustss static char cms_note_table[] = {
912365a92cSaugustss 	/* A  */ 3,
922365a92cSaugustss 	/* A# */ 31,
932365a92cSaugustss 	/* B  */ 58,
942365a92cSaugustss 	/* C  */ 83,
952365a92cSaugustss 	/* C# */ 107,
962365a92cSaugustss 	/* D  */ 130,
972365a92cSaugustss 	/* D# */ 151,
982365a92cSaugustss 	/* E  */ 172,
992365a92cSaugustss 	/* F  */ 191,
1002365a92cSaugustss 	/* F# */ 209,
1012365a92cSaugustss 	/* G  */ 226,
1022365a92cSaugustss 	/* G# */ 242,
1032365a92cSaugustss };
1042365a92cSaugustss 
1052365a92cSaugustss #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
1062365a92cSaugustss #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
1072365a92cSaugustss 
1082365a92cSaugustss int
cms_probe(device_t parent,cfdata_t match,void * aux)1094dccf0aeScube cms_probe(device_t parent, cfdata_t match, void *aux)
1102365a92cSaugustss {
1112365a92cSaugustss 	struct isa_attach_args *ia = aux;
1122365a92cSaugustss 	bus_space_tag_t iot;
1132365a92cSaugustss 	bus_space_handle_t ioh;
1142365a92cSaugustss 	int found = 0;
1152365a92cSaugustss 	int i;
1162365a92cSaugustss 
1172365a92cSaugustss 	DPRINTF(("cms_probe():\n"));
1182365a92cSaugustss 
1192365a92cSaugustss 	iot = ia->ia_iot;
1202365a92cSaugustss 
1213835413bSthorpej 	if (ia->ia_nio < 1)
1222365a92cSaugustss 		return 0;
1232365a92cSaugustss 
1243835413bSthorpej 	if (ISA_DIRECT_CONFIG(ia))
1253835413bSthorpej 		return 0;
1263835413bSthorpej 
1271308c6d7Sdrochner 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
1283835413bSthorpej 		return 0;
1293835413bSthorpej 
1303835413bSthorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh))
1312365a92cSaugustss 		return 0;
1322365a92cSaugustss 
1332365a92cSaugustss 	bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
1342365a92cSaugustss 	if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
1352365a92cSaugustss 		goto out;
1362365a92cSaugustss 
1372365a92cSaugustss 	for (i = 0; i < 8; i++) {
1382365a92cSaugustss 		if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
1392365a92cSaugustss 			goto out;
1402365a92cSaugustss 	}
1412365a92cSaugustss 	found = 1;
1422365a92cSaugustss 
1433835413bSthorpej 	ia->ia_nio = 1;
1443835413bSthorpej 	ia->ia_io[0].ir_size = CMS_IOSIZE;
1453835413bSthorpej 
1463835413bSthorpej 	ia->ia_niomem = 0;
1473835413bSthorpej 	ia->ia_nirq = 0;
1483835413bSthorpej 	ia->ia_ndrq = 0;
1492365a92cSaugustss 
1502365a92cSaugustss out:
1512365a92cSaugustss 	bus_space_unmap(iot, ioh, CMS_IOSIZE);
1522365a92cSaugustss 
1532365a92cSaugustss 	return found;
1542365a92cSaugustss }
1552365a92cSaugustss 
1562365a92cSaugustss 
1572365a92cSaugustss void
cms_attach(device_t parent,device_t self,void * aux)1584dccf0aeScube cms_attach(device_t parent, device_t self, void *aux)
1592365a92cSaugustss {
1604dccf0aeScube 	struct cms_softc *sc = device_private(self);
1612365a92cSaugustss 	struct isa_attach_args *ia = aux;
1622365a92cSaugustss 	bus_space_tag_t iot;
1632365a92cSaugustss 	bus_space_handle_t ioh;
1642365a92cSaugustss 	midisyn *ms;
1652365a92cSaugustss 
166ca6ee1e0Sjakllsch 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_AUDIO);
1674dccf0aeScube 
1684dccf0aeScube 	aprint_normal("\n");
1692365a92cSaugustss 
1702365a92cSaugustss 	DPRINTF(("cms_attach():\n"));
1712365a92cSaugustss 
1722365a92cSaugustss 	iot = ia->ia_iot;
1732365a92cSaugustss 
1743835413bSthorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh)) {
1754dccf0aeScube 		aprint_error_dev(self, "can't map i/o space\n");
1762365a92cSaugustss 		return;
1772365a92cSaugustss 	}
1782365a92cSaugustss 
1792365a92cSaugustss 	sc->sc_iot = iot;
1802365a92cSaugustss 	sc->sc_ioh = ioh;
1812365a92cSaugustss 
1822365a92cSaugustss 	/* now let's reset the chips */
1832365a92cSaugustss 	cms_reset(sc);
1842365a92cSaugustss 
1851687854cSplunky 	/* init the synthesiser */
1862365a92cSaugustss 	ms = &sc->sc_midisyn;
1872365a92cSaugustss 	ms->mets = &midi_cms_hw;
1882365a92cSaugustss 	strcpy(ms->name, "Creative Music System");
1892365a92cSaugustss 	ms->nvoice = CMS_NVOICES;
1902365a92cSaugustss 	ms->data = sc;
1918a962f23Sjmcneill 	ms->lock = &sc->sc_lock;
1921687854cSplunky 	midisyn_init(ms);
1932365a92cSaugustss 
1941687854cSplunky 	/* and attach the midi device with the synthesiser */
1951687854cSplunky 	midi_attach_mi(&midisyn_hw_if, ms, self);
1962365a92cSaugustss }
1972365a92cSaugustss 
1982365a92cSaugustss 
1992365a92cSaugustss int
cms_open(midisyn * ms,int flag)200168cd830Schristos cms_open(midisyn *ms, int flag)
2012365a92cSaugustss {
2022365a92cSaugustss 	struct cms_softc *sc = (struct cms_softc *)ms->data;
2032365a92cSaugustss 
2042365a92cSaugustss 	cms_reset(sc);
2052365a92cSaugustss 
2062365a92cSaugustss 	return 0;
2072365a92cSaugustss }
2082365a92cSaugustss 
2092365a92cSaugustss void
cms_close(midisyn * ms)2104dccf0aeScube cms_close(midisyn *ms)
2112365a92cSaugustss {
2122365a92cSaugustss 	struct cms_softc *sc = (struct cms_softc *)ms->data;
2132365a92cSaugustss 
2142365a92cSaugustss 	cms_reset(sc);
2152365a92cSaugustss }
2162365a92cSaugustss 
2172365a92cSaugustss void
cms_on(midisyn * ms,uint_fast16_t vidx,midipitch_t mp,int16_t level_cB)218710af634Schap cms_on(midisyn *ms, uint_fast16_t vidx, midipitch_t mp, int16_t level_cB)
2192365a92cSaugustss {
2202365a92cSaugustss 	struct cms_softc *sc = (struct cms_softc *)ms->data;
221710af634Schap 	int chip = CHAN_TO_CHIP(vidx);
222710af634Schap 	int voice = CHAN_TO_VOICE(vidx);
223710af634Schap 	uint32_t note;
2242365a92cSaugustss 	u_int8_t octave;
2252365a92cSaugustss 	u_int8_t count;
2262365a92cSaugustss 	u_int8_t reg;
2272365a92cSaugustss 	u_int8_t vol;
2282365a92cSaugustss 
229710af634Schap 	/*
230710af634Schap 	 * The next line is a regrettable hack, because it drops all pitch
231710af634Schap 	 * adjustment midisyn has supplied in miditune, so this synth will
232710af634Schap 	 * not respond to tuning, pitchbend, etc. It seems it ought to be
233710af634Schap 	 * possible to DTRT if the formula that generated the cms_note_table
234710af634Schap 	 * can be found, but I've had no luck, and a plot of the numbers in
235710af634Schap 	 * the table clearly curves the wrong way to be derived any obvious
236710af634Schap 	 * way from the equal tempered scale. (Or maybe the table's wrong?
237710af634Schap 	 * Has this device been playing flat up the scale? I don't have
238710af634Schap 	 * access to one to try.)
239710af634Schap 	 */
240710af634Schap 	note = MIDIPITCH_TO_KEY(mp);
241710af634Schap 
2422365a92cSaugustss 	if (note < CMS_FIRST_NOTE)
2432365a92cSaugustss 		return;
2442365a92cSaugustss 
2452365a92cSaugustss 	octave = NOTE_TO_OCTAVE(note);
2462365a92cSaugustss 	count = NOTE_TO_COUNT(note);
2472365a92cSaugustss 
2482365a92cSaugustss 	DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
2492365a92cSaugustss 		chip, voice, octave, count, OCTAVE_OFFSET(voice),
2502365a92cSaugustss 		OCTAVE_SHIFT(voice)));
2512365a92cSaugustss 
2522365a92cSaugustss 	/* write the count */
2532365a92cSaugustss 	CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
2542365a92cSaugustss 
2552365a92cSaugustss 	/* select the octave */
2562365a92cSaugustss 	reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
2572365a92cSaugustss 	reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
2582365a92cSaugustss 	reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
2592365a92cSaugustss 	CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
2602365a92cSaugustss 
2612365a92cSaugustss 	/* set the volume */
262710af634Schap 	/* this may be the wrong curve but will do something. no docs! */
26344e7baedSmaya 	vol = 15 + ((level_cB > -75) ? level_cB/5 : -15);
2642365a92cSaugustss 	CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
2652365a92cSaugustss 
2662365a92cSaugustss 	/* enable the voice */
2672365a92cSaugustss 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
2682365a92cSaugustss 	reg |= (1<<voice);
2692365a92cSaugustss 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
2702365a92cSaugustss }
2712365a92cSaugustss 
2722365a92cSaugustss void
cms_off(midisyn * ms,uint_fast16_t vidx,uint_fast8_t vel)273168cd830Schristos cms_off(midisyn *ms, uint_fast16_t vidx, uint_fast8_t vel)
2742365a92cSaugustss {
2752365a92cSaugustss 	struct cms_softc *sc = (struct cms_softc *)ms->data;
276710af634Schap 	int chip = CHAN_TO_CHIP(vidx);
277710af634Schap 	int voice = CHAN_TO_VOICE(vidx);
2782365a92cSaugustss 	u_int8_t reg;
2792365a92cSaugustss 
2802365a92cSaugustss 	/* disable the channel */
2812365a92cSaugustss 	reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
2822365a92cSaugustss 	reg &= ~(1<<voice);
2832365a92cSaugustss 	CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
2842365a92cSaugustss }
2852365a92cSaugustss 
2862365a92cSaugustss static void
cms_reset(struct cms_softc * sc)2874dccf0aeScube cms_reset(struct cms_softc *sc)
2882365a92cSaugustss {
2892365a92cSaugustss 	int i;
2902365a92cSaugustss 
2912365a92cSaugustss 	DPRINTF(("cms_reset():\n"));
2922365a92cSaugustss 
2932365a92cSaugustss 	for (i = 0; i < 6; i++) {
2942365a92cSaugustss 		CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
2952365a92cSaugustss 		CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
2962365a92cSaugustss 
2972365a92cSaugustss 		CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
2982365a92cSaugustss 		CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
2992365a92cSaugustss 	}
3002365a92cSaugustss 
3012365a92cSaugustss 	for (i = 0; i < 3; i++) {
3022365a92cSaugustss 		CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
3032365a92cSaugustss 		CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
3042365a92cSaugustss 	}
3052365a92cSaugustss 
3062365a92cSaugustss 	CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
3072365a92cSaugustss 	CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
3082365a92cSaugustss 
3092365a92cSaugustss 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
3102365a92cSaugustss 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
3112365a92cSaugustss 
3122365a92cSaugustss 	CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
3132365a92cSaugustss 	CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
3142365a92cSaugustss 
3152365a92cSaugustss 	/*
3162365a92cSaugustss 	 * These registers don't appear to be useful, but must be
3172365a92cSaugustss 	 * cleared otherwise certain voices don't work properly
3182365a92cSaugustss 	 */
3192365a92cSaugustss 	CMS_WRITE(sc, 0, 0x18, 0x00);
3202365a92cSaugustss 	CMS_WRITE(sc, 1, 0x18, 0x00);
3212365a92cSaugustss 	CMS_WRITE(sc, 0, 0x19, 0x00);
3222365a92cSaugustss 	CMS_WRITE(sc, 1, 0x19, 0x00);
3232365a92cSaugustss 
3242365a92cSaugustss 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
3252365a92cSaugustss 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
3262365a92cSaugustss 
3272365a92cSaugustss 	CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
3282365a92cSaugustss 	CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
3292365a92cSaugustss }
330