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