Home | History | Annotate | Line # | Download | only in ic
mpu.c revision 1.15.16.1
      1  1.15.16.1       mjf /*	$NetBSD: mpu.c,v 1.15.16.1 2008/04/03 12:42:41 mjf 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.15.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: mpu.c,v 1.15.16.1 2008/04/03 12:42:41 mjf 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.15        ad #include <sys/cpu.h>
     52       1.15        ad #include <sys/intr.h>
     53       1.15        ad #include <sys/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.15.16.1       mjf static int 		mpu_reset(struct mpu_softc *);
     82       1.12     perry static	inline int mpu_waitready(struct mpu_softc *);
     83  1.15.16.1       mjf static void		mpu_readinput(struct mpu_softc *);
     84        1.1  augustss 
     85  1.15.16.1       mjf static int	mpu_open(void *, int,
     86        1.9     perry 			 void (*iintr)(void *, int),
     87        1.9     perry 			 void (*ointr)(void *), void *arg);
     88  1.15.16.1       mjf static void	mpu_close(void *);
     89  1.15.16.1       mjf static int	mpu_output(void *, int);
     90  1.15.16.1       mjf static void	mpu_getinfo(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.15.16.1       mjf mpu_find(struct mpu_softc *sc)
    102        1.1  augustss {
    103        1.1  augustss 	if (MPU_GETSTATUS(sc->iot, sc->ioh) == 0xff) {
    104  1.15.16.1       mjf 		DPRINTF(("%s: No status\n", __func__));
    105        1.1  augustss 		goto bad;
    106        1.1  augustss 	}
    107        1.1  augustss 	sc->open = 0;
    108        1.1  augustss 	sc->intr = 0;
    109        1.1  augustss 	if (mpu_reset(sc) == 0)
    110        1.1  augustss 		return 1;
    111        1.1  augustss bad:
    112        1.1  augustss 	return 0;
    113        1.1  augustss }
    114        1.1  augustss 
    115        1.2  augustss void
    116  1.15.16.1       mjf mpu_attach(struct mpu_softc *sc)
    117        1.2  augustss {
    118  1.15.16.1       mjf 	midi_attach_mi(&mpu_midi_hw_if, sc, sc->sc_dev);
    119        1.2  augustss }
    120        1.2  augustss 
    121       1.12     perry static inline int
    122  1.15.16.1       mjf mpu_waitready(struct mpu_softc *sc)
    123        1.1  augustss {
    124        1.1  augustss 	int i;
    125        1.1  augustss 
    126        1.1  augustss 	for(i = 0; i < MPU_MAXWAIT; i++) {
    127        1.1  augustss 		if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_OUTPUT_BUSY))
    128        1.1  augustss 			return 0;
    129        1.1  augustss 		delay(10);
    130        1.1  augustss 	}
    131        1.1  augustss 	return 1;
    132        1.1  augustss }
    133        1.1  augustss 
    134  1.15.16.1       mjf static int
    135  1.15.16.1       mjf mpu_reset(struct mpu_softc *sc)
    136        1.1  augustss {
    137        1.1  augustss 	bus_space_tag_t iot = sc->iot;
    138        1.1  augustss 	bus_space_handle_t ioh = sc->ioh;
    139        1.1  augustss 	int i;
    140        1.1  augustss 	int s;
    141        1.1  augustss 
    142        1.1  augustss 	if (mpu_waitready(sc)) {
    143  1.15.16.1       mjf 		DPRINTF(("%s: not ready\n", __func__));
    144        1.1  augustss 		return EIO;
    145        1.1  augustss 	}
    146        1.1  augustss 	s = splaudio();		/* Don't let the interrupt get our ACK. */
    147        1.1  augustss 	bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
    148        1.1  augustss 	for(i = 0; i < 2*MPU_MAXWAIT; i++) {
    149        1.1  augustss 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
    150        1.1  augustss 		    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
    151        1.1  augustss 			splx(s);
    152        1.1  augustss 			return 0;
    153        1.1  augustss 		}
    154        1.1  augustss 	}
    155        1.1  augustss 	splx(s);
    156  1.15.16.1       mjf 	DPRINTF(("%s: No ACK\n", __func__));
    157        1.1  augustss 	return EIO;
    158        1.1  augustss }
    159        1.1  augustss 
    160  1.15.16.1       mjf static int
    161       1.14  christos mpu_open(void *addr, int flags, void (*iintr)(void *, int),
    162       1.14  christos     void (*ointr)(void *), void *arg)
    163        1.1  augustss {
    164        1.1  augustss 	struct mpu_softc *sc = addr;
    165        1.1  augustss 
    166  1.15.16.1       mjf         DPRINTF(("%s: sc=%p\n", __func__, sc));
    167        1.1  augustss 
    168        1.1  augustss 	if (sc->open)
    169        1.1  augustss 		return EBUSY;
    170        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    171        1.3     itohy 	if (sc->powerctl)
    172        1.3     itohy 		sc->powerctl(sc->powerarg, 1);
    173        1.3     itohy #endif
    174        1.3     itohy 	if (mpu_reset(sc) != 0) {
    175        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    176        1.3     itohy 		if (sc->powerctl)
    177        1.3     itohy 			sc->powerctl(sc->powerarg, 0);
    178        1.3     itohy #endif
    179        1.1  augustss 		return EIO;
    180        1.3     itohy 	}
    181        1.1  augustss 
    182        1.1  augustss 	bus_space_write_1(sc->iot, sc->ioh, MPU_COMMAND, MPU_UART_MODE);
    183        1.1  augustss 	sc->open = 1;
    184        1.1  augustss 	sc->intr = iintr;
    185        1.1  augustss 	sc->arg = arg;
    186        1.1  augustss 	return 0;
    187        1.1  augustss }
    188        1.1  augustss 
    189  1.15.16.1       mjf static void
    190  1.15.16.1       mjf mpu_close(void *addr)
    191        1.1  augustss {
    192        1.1  augustss 	struct mpu_softc *sc = addr;
    193        1.1  augustss 
    194  1.15.16.1       mjf         DPRINTF(("%s: sc=%p\n", __func__, sc));
    195        1.1  augustss 
    196        1.1  augustss 	sc->open = 0;
    197        1.1  augustss 	sc->intr = 0;
    198        1.1  augustss 	mpu_reset(sc); /* exit UART mode */
    199        1.3     itohy 
    200        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    201        1.3     itohy 	if (sc->powerctl)
    202        1.3     itohy 		sc->powerctl(sc->powerarg, 0);
    203        1.3     itohy #endif
    204        1.1  augustss }
    205        1.1  augustss 
    206  1.15.16.1       mjf static void
    207  1.15.16.1       mjf mpu_readinput(struct mpu_softc *sc)
    208        1.1  augustss {
    209        1.1  augustss 	bus_space_tag_t iot = sc->iot;
    210        1.1  augustss 	bus_space_handle_t ioh = sc->ioh;
    211        1.1  augustss 	int data;
    212        1.1  augustss 
    213        1.1  augustss 	while(!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY)) {
    214        1.1  augustss 		data = bus_space_read_1(iot, ioh, MPU_DATA);
    215  1.15.16.1       mjf 		DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, sc, data));
    216        1.1  augustss 		if (sc->intr)
    217        1.1  augustss 			sc->intr(sc->arg, data);
    218        1.1  augustss 	}
    219        1.1  augustss }
    220        1.1  augustss 
    221  1.15.16.1       mjf static int
    222  1.15.16.1       mjf mpu_output(void *addr, int d)
    223        1.1  augustss {
    224        1.1  augustss 	struct mpu_softc *sc = addr;
    225        1.1  augustss 	int s;
    226        1.1  augustss 
    227  1.15.16.1       mjf 	DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, sc, d));
    228        1.1  augustss 	if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY)) {
    229        1.1  augustss 		s = splaudio();
    230        1.1  augustss 		mpu_readinput(sc);
    231        1.1  augustss 		splx(s);
    232        1.1  augustss 	}
    233        1.1  augustss 	if (mpu_waitready(sc)) {
    234  1.15.16.1       mjf 		DPRINTF(("%s:: not ready\n", __func__));
    235        1.1  augustss 		return EIO;
    236        1.1  augustss 	}
    237        1.1  augustss 	bus_space_write_1(sc->iot, sc->ioh, MPU_DATA, d);
    238        1.1  augustss 	return 0;
    239        1.1  augustss }
    240        1.1  augustss 
    241  1.15.16.1       mjf static void
    242  1.15.16.1       mjf mpu_getinfo(void *addr, struct midi_info *mi)
    243        1.1  augustss {
    244        1.2  augustss 	struct mpu_softc *sc = addr;
    245        1.1  augustss 
    246        1.2  augustss 	mi->name = sc->model;
    247        1.1  augustss 	mi->props = 0;
    248        1.1  augustss }
    249        1.1  augustss 
    250        1.1  augustss int
    251  1.15.16.1       mjf mpu_intr(void *addr)
    252        1.1  augustss {
    253        1.1  augustss 	struct mpu_softc *sc = addr;
    254        1.1  augustss 
    255        1.1  augustss 	if (MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY) {
    256  1.15.16.1       mjf 		DPRINTF(("%s: no data\n", __func__));
    257  1.15.16.1       mjf 		return 0;
    258        1.1  augustss 	} else {
    259        1.1  augustss 		mpu_readinput(sc);
    260  1.15.16.1       mjf 		return 1;
    261        1.1  augustss 	}
    262        1.1  augustss }
    263