Home | History | Annotate | Line # | Download | only in dev
opm.c revision 1.10.6.2
      1 /*	$NetBSD: opm.c,v 1.10.6.2 2004/09/18 14:42:26 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Masanobu Saitoh, Takuya Harakawa.
      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 Masanobu Saitoh.
     18  * 4. Neither the name of the University nor of the Laboratory may be used
     19  *    to endorse or promote products derived from this software without
     20  *    specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Temporary implementation: not fully bus.h'fied.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: opm.c,v 1.10.6.2 2004/09/18 14:42:26 skrll Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/cpu.h>
     48 
     49 #include <machine/opmreg.h>
     50 #include <arch/x68k/dev/opmvar.h>
     51 #include <arch/x68k/dev/intiovar.h>
     52 
     53 struct opm_softc {
     54 	struct device		sc_dev;
     55 
     56 	bus_space_tag_t		sc_bst;
     57 	bus_space_handle_t	sc_bht;
     58 	u_int8_t		sc_regs[0x100];
     59 	struct opm_voice	sc_vdata[8];
     60 };
     61 
     62 struct opm_softc	*opm0;	/* XXX */
     63 
     64 static int opm_match __P((struct device *, struct cfdata *, void *));
     65 static void opm_attach __P((struct device *, struct device *, void *));
     66 
     67 CFATTACH_DECL(opm, sizeof (struct opm_softc),
     68     opm_match, opm_attach, NULL, NULL);
     69 
     70 static int
     71 opm_match(parent, cf, aux)
     72 	struct device *parent;
     73 	struct cfdata *cf;
     74 	void *aux;
     75 {
     76 	struct intio_attach_args *ia = aux;
     77 
     78 	if (strcmp(ia->ia_name, "opm") != 0)
     79 		return 0;
     80 
     81 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
     82 		ia->ia_addr = 0xe90000;
     83 	ia->ia_size = 0x2000;
     84 	if (intio_map_allocate_region (parent, ia, INTIO_MAP_TESTONLY))
     85 		return 0;
     86 
     87 	return 1;
     88 }
     89 
     90 static void
     91 opm_attach(parent, self, aux)
     92 	struct device *parent, *self;
     93 	void *aux;
     94 {
     95 	struct opm_softc *sc = (struct opm_softc *)self;
     96 	struct intio_attach_args *ia = aux;
     97 	int r;
     98 
     99 	printf ("\n");
    100 	ia->ia_size = 0x2000;
    101 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    102 #ifdef DIAGNOSTIC
    103 	if (r)
    104 		panic ("IO map for OPM corruption??");
    105 #endif
    106 
    107 	sc->sc_bst = ia->ia_bst;
    108 	r = bus_space_map (sc->sc_bst,
    109 			   ia->ia_addr, ia->ia_size,
    110 			   BUS_SPACE_MAP_SHIFTED,
    111 			   &sc->sc_bht);
    112 #ifdef DIAGNOSTIC
    113 	if (r)
    114 		panic ("Cannot map IO space for OPM.");
    115 #endif
    116 
    117 	if (sc->sc_dev.dv_unit == 0)
    118 		opm0 = sc;	/* XXX */
    119 
    120 	return;
    121 }
    122 
    123 void opm_set_volume __P((int, int));
    124 void opm_set_key __P((int, int));
    125 void opm_set_voice __P((int, struct opm_voice *));
    126 void opm_set_voice_sub __P((int, struct opm_operator *));
    127 __inline static void writeopm __P((int, int));
    128 __inline static int readopm __P((int));
    129 void opm_key_on __P((u_char));
    130 void opm_key_off __P((u_char));
    131 int opmopen __P((dev_t, int, int));
    132 int opmclose __P((dev_t));
    133 
    134 __inline static void
    135 writeopm(reg, dat)
    136 	int reg, dat;
    137 {
    138 	while (bus_space_read_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80);
    139 	bus_space_write_1 (opm0->sc_bst, opm0->sc_bht, OPM_REG, reg);
    140 	while (bus_space_read_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80);
    141 	bus_space_write_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA, dat);
    142 	opm0->sc_regs[reg] = dat;
    143 }
    144 
    145 __inline static int
    146 readopm(reg)
    147 	int reg;
    148 {
    149 	return opm0->sc_regs[reg];
    150 }
    151 
    152 #include "fd.h"
    153 #include "vs.h"
    154 #include "bell.h"
    155 
    156 #if NVS > 0
    157 void
    158 adpcm_chgclk(clk)
    159 	u_char	clk;
    160 {
    161 	writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT1MSK) | clk);
    162 }
    163 #endif
    164 
    165 #if NFD > 0
    166 void
    167 fdc_force_ready(rdy)
    168 	u_char	rdy;
    169 {
    170 	writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT2MSK) | rdy);
    171 }
    172 #endif
    173 
    174 #if NBELL > 0
    175 void
    176 opm_key_on(channel)
    177 	u_char channel;
    178 {
    179     writeopm(0x08, opm0->sc_vdata[channel].sm << 3 | channel);
    180 }
    181 
    182 void
    183 opm_key_off(channel)
    184 	u_char	channel;
    185 {
    186     writeopm(0x08, channel);
    187 }
    188 
    189 void
    190 opm_set_voice(channel, voice)
    191 	int channel;
    192 	struct opm_voice *voice;
    193 {
    194 	memcpy(&opm0->sc_vdata[channel], voice, sizeof(struct opm_voice));
    195 
    196 	opm_set_voice_sub(0x40 + channel, &voice->m1);
    197 	opm_set_voice_sub(0x48 + channel, &voice->m2);
    198 	opm_set_voice_sub(0x50 + channel, &voice->c1);
    199 	opm_set_voice_sub(0x58 + channel, &voice->c2);
    200 	writeopm(0x20 + channel, 0xc0 | (voice->fb & 0x7) << 3 | (voice->con & 0x7));
    201 }
    202 
    203 void
    204 opm_set_voice_sub(reg, op)
    205 	register int reg;
    206 	struct opm_operator *op;
    207 {
    208     /* DT1/MUL */
    209     writeopm(reg, (op->dt1 & 0x7) << 3 | (op->mul & 0x7));
    210 
    211     /* TL */
    212     writeopm(reg + 0x20, op->tl & 0x7f);
    213 
    214     /* KS/AR */
    215     writeopm(reg + 0x40, (op->ks & 0x3) << 6 | (op->ar & 0x1f));
    216 
    217     /* AMS/D1R */
    218     writeopm(reg + 0x60, (op->ame & 0x1) << 7 | (op->d1r & 0x1f));
    219 
    220     /* DT2/D2R */
    221     writeopm(reg + 0x80, (op->dt2 & 0x3) << 6 | (op->d2r & 0x1f));
    222 
    223     /* D1L/RR */
    224     writeopm(reg + 0xa0, (op->d1l & 0xf) << 4 | (op->rr & 0xf));
    225 }
    226 
    227 void
    228 opm_set_volume(channel, volume)
    229 	int channel;
    230 	int volume;
    231 {
    232     int value;
    233 
    234     switch (opm0->sc_vdata[channel].con) {
    235     case 7:
    236 	value = opm0->sc_vdata[channel].m1.tl + volume;
    237 	writeopm(0x60 + channel, ((value > 0x7f) ? 0x7f : value));
    238     case 6:
    239     case 5:
    240 	value = opm0->sc_vdata[channel].m2.tl + volume;
    241 	writeopm(0x68 + channel, ((value > 0x7f) ? 0x7f : value));
    242     case 4:
    243 	value = opm0->sc_vdata[channel].c1.tl + volume;
    244 	writeopm(0x70 + channel, ((value > 0x7f) ? 0x7f : value));
    245     case 3:
    246     case 2:
    247     case 1:
    248     case 0:
    249 	value = opm0->sc_vdata[channel].c2.tl + volume;
    250 	writeopm(0x78 + channel, ((value > 0x7f) ? 0x7f : value));
    251     }
    252 }
    253 
    254 void
    255 opm_set_key(channel, tone)
    256 	int channel;
    257 	int tone;
    258 {
    259 	writeopm(0x28 + channel, tone >> 8);
    260 	writeopm(0x30 + channel, tone & 0xff);
    261 }
    262 
    263 /*ARGSUSED*/
    264 int
    265 opmopen(dev, flag, mode)
    266 	dev_t dev;
    267 	int flag, mode;
    268 {
    269 	return 0;
    270 }
    271 
    272 /*ARGSUSED*/
    273 int
    274 opmclose(dev)
    275 	dev_t dev;
    276 {
    277 	return 0;
    278 }
    279 
    280 #endif
    281