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