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