Home | History | Annotate | Line # | Download | only in ic
mpu.c revision 1.1
      1 /*	$NetBSD: mpu.c,v 1.1 1999/08/01 18:05:39 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/syslog.h>
     44 #include <sys/device.h>
     45 #include <sys/proc.h>
     46 #include <sys/buf.h>
     47 #include <vm/vm.h>
     48 
     49 #include <machine/cpu.h>
     50 #include <machine/intr.h>
     51 #include <machine/bus.h>
     52 
     53 #include <dev/midi_if.h>
     54 
     55 #include <dev/ic/mpuvar.h>
     56 
     57 #ifdef AUDIO_DEBUG
     58 #define DPRINTF(x)	if (mpudebug) printf x
     59 #define DPRINTFN(n,x)	if (mpudebug >= (n)) printf x
     60 int	mpudebug = 0;
     61 #else
     62 #define DPRINTF(x)
     63 #define DPRINTFN(n,x)
     64 #endif
     65 
     66 #define MPU_DATA		0
     67 #define MPU_COMMAND	1
     68 #define  MPU_RESET	0xff
     69 #define  MPU_UART_MODE	0x3f
     70 #define  MPU_ACK		0xfe
     71 #define MPU_STATUS	1
     72 #define  MPU_OUTPUT_BUSY	0x40
     73 #define  MPU_INPUT_EMPTY	0x80
     74 
     75 #define MPU_MAXWAIT	10000	/* usec/10 to wait */
     76 
     77 #define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
     78 
     79 int	mpu_reset(struct mpu_softc *);
     80 static	__inline int mpu_waitready(struct mpu_softc *);
     81 void	mpu_readinput(struct mpu_softc *);
     82 
     83 int	mpu_open __P((void *, int,
     84 			 void (*iintr)__P((void *, int)),
     85 			 void (*ointr)__P((void *)), void *arg));
     86 void	mpu_close __P((void *));
     87 int	mpu_output __P((void *, int));
     88 void	mpu_getinfo __P((void *, struct midi_info *));
     89 
     90 struct midi_hw_if mpu_midi_hw_if = {
     91 	mpu_open,
     92 	mpu_close,
     93 	mpu_output,
     94 	mpu_getinfo,
     95 	0,			/* ioctl */
     96 };
     97 
     98 int
     99 mpu_find(sc)
    100 	struct mpu_softc *sc;
    101 {
    102 	if (MPU_GETSTATUS(sc->iot, sc->ioh) == 0xff) {
    103 		DPRINTF(("mpu_find: No status\n"));
    104 		goto bad;
    105 	}
    106 	sc->open = 0;
    107 	sc->intr = 0;
    108 	if (mpu_reset(sc) == 0)
    109 		return 1;
    110 bad:
    111 	return 0;
    112 }
    113 
    114 static __inline int
    115 mpu_waitready(sc)
    116 	struct mpu_softc *sc;
    117 {
    118 	int i;
    119 
    120 	for(i = 0; i < MPU_MAXWAIT; i++) {
    121 		if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_OUTPUT_BUSY))
    122 			return 0;
    123 		delay(10);
    124 	}
    125 	return 1;
    126 }
    127 
    128 int
    129 mpu_reset(sc)
    130 	struct mpu_softc *sc;
    131 {
    132 	bus_space_tag_t iot = sc->iot;
    133 	bus_space_handle_t ioh = sc->ioh;
    134 	int i;
    135 	int s;
    136 
    137 	if (mpu_waitready(sc)) {
    138 		DPRINTF(("mpu_reset: not ready\n"));
    139 		return EIO;
    140 	}
    141 	s = splaudio();		/* Don't let the interrupt get our ACK. */
    142 	bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
    143 	for(i = 0; i < 2*MPU_MAXWAIT; i++) {
    144 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
    145 		    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
    146 			splx(s);
    147 			return 0;
    148 		}
    149 	}
    150 	splx(s);
    151 	DPRINTF(("mpu_reset: No ACK\n"));
    152 	return EIO;
    153 }
    154 
    155 int
    156 mpu_open(addr, flags, iintr, ointr, arg)
    157 	void *addr;
    158 	int flags;
    159 	void (*iintr)__P((void *, int));
    160 	void (*ointr)__P((void *));
    161 	void *arg;
    162 {
    163 	struct mpu_softc *sc = addr;
    164 
    165         DPRINTF(("mpu_open: sc=%p\n", sc));
    166 
    167 	if (sc->open)
    168 		return EBUSY;
    169 	if (mpu_reset(sc) != 0)
    170 		return EIO;
    171 
    172 	bus_space_write_1(sc->iot, sc->ioh, MPU_COMMAND, MPU_UART_MODE);
    173 	sc->open = 1;
    174 	sc->intr = iintr;
    175 	sc->arg = arg;
    176 	return 0;
    177 }
    178 
    179 void
    180 mpu_close(addr)
    181 	void *addr;
    182 {
    183 	struct mpu_softc *sc = addr;
    184 
    185         DPRINTF(("mpu_close: sc=%p\n", sc));
    186 
    187 	sc->open = 0;
    188 	sc->intr = 0;
    189 	mpu_reset(sc); /* exit UART mode */
    190 }
    191 
    192 void
    193 mpu_readinput(sc)
    194 	struct mpu_softc *sc;
    195 {
    196 	bus_space_tag_t iot = sc->iot;
    197 	bus_space_handle_t ioh = sc->ioh;
    198 	int data;
    199 
    200 	while(!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY)) {
    201 		data = bus_space_read_1(iot, ioh, MPU_DATA);
    202 		DPRINTFN(3, ("mpu_rea: sc=%p 0x%02x\n", sc, data));
    203 		if (sc->intr)
    204 			sc->intr(sc->arg, data);
    205 	}
    206 }
    207 
    208 int
    209 mpu_output(addr, d)
    210 	void *addr;
    211 	int d;
    212 {
    213 	struct mpu_softc *sc = addr;
    214 	int s;
    215 
    216 	DPRINTFN(3, ("mpu_output: sc=%p 0x%02x\n", sc, d));
    217 	if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY)) {
    218 		s = splaudio();
    219 		mpu_readinput(sc);
    220 		splx(s);
    221 	}
    222 	if (mpu_waitready(sc)) {
    223 		DPRINTF(("mpu_output: not ready\n"));
    224 		return EIO;
    225 	}
    226 	bus_space_write_1(sc->iot, sc->ioh, MPU_DATA, d);
    227 	return 0;
    228 }
    229 
    230 void
    231 mpu_getinfo(addr, mi)
    232 	void *addr;
    233 	struct midi_info *mi;
    234 {
    235 
    236 	mi->name = "MPU-401 MIDI UART";
    237 	mi->props = 0;
    238 }
    239 
    240 int
    241 mpu_intr(addr)
    242 	void *addr;
    243 {
    244 	struct mpu_softc *sc = addr;
    245 
    246 	if (MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY) {
    247 		DPRINTF(("mpu_intr: no data\n"));
    248 		return (0);
    249 	} else {
    250 		mpu_readinput(sc);
    251 		return (1);
    252 	}
    253 }
    254