Home | History | Annotate | Line # | Download | only in ic
mpu.c revision 1.15.16.2
      1  1.15.16.1       mjf /*	$NetBSD: mpu.c,v 1.15.16.2 2008/06/02 13:23:25 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  *
     19        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1  augustss  */
     31        1.5     lukem 
     32        1.5     lukem #include <sys/cdefs.h>
     33  1.15.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: mpu.c,v 1.15.16.2 2008/06/02 13:23:25 mjf Exp $");
     34        1.1  augustss 
     35        1.1  augustss #include <sys/param.h>
     36        1.1  augustss #include <sys/systm.h>
     37        1.1  augustss #include <sys/errno.h>
     38        1.1  augustss #include <sys/ioctl.h>
     39        1.1  augustss #include <sys/syslog.h>
     40        1.1  augustss #include <sys/device.h>
     41        1.1  augustss #include <sys/proc.h>
     42        1.1  augustss #include <sys/buf.h>
     43        1.1  augustss 
     44       1.15        ad #include <sys/cpu.h>
     45       1.15        ad #include <sys/intr.h>
     46       1.15        ad #include <sys/bus.h>
     47        1.1  augustss 
     48        1.1  augustss #include <dev/midi_if.h>
     49        1.1  augustss 
     50        1.1  augustss #include <dev/ic/mpuvar.h>
     51        1.1  augustss 
     52        1.1  augustss #ifdef AUDIO_DEBUG
     53        1.1  augustss #define DPRINTF(x)	if (mpudebug) printf x
     54        1.1  augustss #define DPRINTFN(n,x)	if (mpudebug >= (n)) printf x
     55        1.1  augustss int	mpudebug = 0;
     56        1.1  augustss #else
     57        1.1  augustss #define DPRINTF(x)
     58        1.1  augustss #define DPRINTFN(n,x)
     59        1.1  augustss #endif
     60        1.1  augustss 
     61        1.1  augustss #define MPU_DATA		0
     62        1.1  augustss #define MPU_COMMAND	1
     63        1.1  augustss #define  MPU_RESET	0xff
     64        1.1  augustss #define  MPU_UART_MODE	0x3f
     65        1.1  augustss #define  MPU_ACK		0xfe
     66        1.1  augustss #define MPU_STATUS	1
     67        1.1  augustss #define  MPU_OUTPUT_BUSY	0x40
     68        1.1  augustss #define  MPU_INPUT_EMPTY	0x80
     69        1.1  augustss 
     70        1.1  augustss #define MPU_MAXWAIT	10000	/* usec/10 to wait */
     71        1.1  augustss 
     72        1.1  augustss #define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
     73        1.1  augustss 
     74  1.15.16.1       mjf static int 		mpu_reset(struct mpu_softc *);
     75       1.12     perry static	inline int mpu_waitready(struct mpu_softc *);
     76  1.15.16.1       mjf static void		mpu_readinput(struct mpu_softc *);
     77        1.1  augustss 
     78  1.15.16.1       mjf static int	mpu_open(void *, int,
     79        1.9     perry 			 void (*iintr)(void *, int),
     80        1.9     perry 			 void (*ointr)(void *), void *arg);
     81  1.15.16.1       mjf static void	mpu_close(void *);
     82  1.15.16.1       mjf static int	mpu_output(void *, int);
     83  1.15.16.1       mjf static void	mpu_getinfo(void *, struct midi_info *);
     84        1.1  augustss 
     85        1.7      yamt const struct midi_hw_if mpu_midi_hw_if = {
     86        1.1  augustss 	mpu_open,
     87        1.1  augustss 	mpu_close,
     88        1.1  augustss 	mpu_output,
     89        1.1  augustss 	mpu_getinfo,
     90        1.1  augustss 	0,			/* ioctl */
     91        1.1  augustss };
     92        1.1  augustss 
     93        1.1  augustss int
     94  1.15.16.1       mjf mpu_find(struct mpu_softc *sc)
     95        1.1  augustss {
     96        1.1  augustss 	if (MPU_GETSTATUS(sc->iot, sc->ioh) == 0xff) {
     97  1.15.16.1       mjf 		DPRINTF(("%s: No status\n", __func__));
     98        1.1  augustss 		goto bad;
     99        1.1  augustss 	}
    100        1.1  augustss 	sc->open = 0;
    101        1.1  augustss 	sc->intr = 0;
    102        1.1  augustss 	if (mpu_reset(sc) == 0)
    103        1.1  augustss 		return 1;
    104        1.1  augustss bad:
    105        1.1  augustss 	return 0;
    106        1.1  augustss }
    107        1.1  augustss 
    108        1.2  augustss void
    109  1.15.16.1       mjf mpu_attach(struct mpu_softc *sc)
    110        1.2  augustss {
    111  1.15.16.1       mjf 	midi_attach_mi(&mpu_midi_hw_if, sc, sc->sc_dev);
    112        1.2  augustss }
    113        1.2  augustss 
    114       1.12     perry static inline int
    115  1.15.16.1       mjf mpu_waitready(struct mpu_softc *sc)
    116        1.1  augustss {
    117        1.1  augustss 	int i;
    118        1.1  augustss 
    119        1.1  augustss 	for(i = 0; i < MPU_MAXWAIT; i++) {
    120        1.1  augustss 		if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_OUTPUT_BUSY))
    121        1.1  augustss 			return 0;
    122        1.1  augustss 		delay(10);
    123        1.1  augustss 	}
    124        1.1  augustss 	return 1;
    125        1.1  augustss }
    126        1.1  augustss 
    127  1.15.16.1       mjf static int
    128  1.15.16.1       mjf mpu_reset(struct mpu_softc *sc)
    129        1.1  augustss {
    130        1.1  augustss 	bus_space_tag_t iot = sc->iot;
    131        1.1  augustss 	bus_space_handle_t ioh = sc->ioh;
    132        1.1  augustss 	int i;
    133        1.1  augustss 	int s;
    134        1.1  augustss 
    135        1.1  augustss 	if (mpu_waitready(sc)) {
    136  1.15.16.1       mjf 		DPRINTF(("%s: not ready\n", __func__));
    137        1.1  augustss 		return EIO;
    138        1.1  augustss 	}
    139        1.1  augustss 	s = splaudio();		/* Don't let the interrupt get our ACK. */
    140        1.1  augustss 	bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
    141        1.1  augustss 	for(i = 0; i < 2*MPU_MAXWAIT; i++) {
    142        1.1  augustss 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
    143        1.1  augustss 		    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
    144        1.1  augustss 			splx(s);
    145        1.1  augustss 			return 0;
    146        1.1  augustss 		}
    147        1.1  augustss 	}
    148        1.1  augustss 	splx(s);
    149  1.15.16.1       mjf 	DPRINTF(("%s: No ACK\n", __func__));
    150        1.1  augustss 	return EIO;
    151        1.1  augustss }
    152        1.1  augustss 
    153  1.15.16.1       mjf static int
    154       1.14  christos mpu_open(void *addr, int flags, void (*iintr)(void *, int),
    155       1.14  christos     void (*ointr)(void *), void *arg)
    156        1.1  augustss {
    157        1.1  augustss 	struct mpu_softc *sc = addr;
    158        1.1  augustss 
    159  1.15.16.1       mjf         DPRINTF(("%s: sc=%p\n", __func__, sc));
    160        1.1  augustss 
    161        1.1  augustss 	if (sc->open)
    162        1.1  augustss 		return EBUSY;
    163        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    164        1.3     itohy 	if (sc->powerctl)
    165        1.3     itohy 		sc->powerctl(sc->powerarg, 1);
    166        1.3     itohy #endif
    167        1.3     itohy 	if (mpu_reset(sc) != 0) {
    168        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    169        1.3     itohy 		if (sc->powerctl)
    170        1.3     itohy 			sc->powerctl(sc->powerarg, 0);
    171        1.3     itohy #endif
    172        1.1  augustss 		return EIO;
    173        1.3     itohy 	}
    174        1.1  augustss 
    175        1.1  augustss 	bus_space_write_1(sc->iot, sc->ioh, MPU_COMMAND, MPU_UART_MODE);
    176        1.1  augustss 	sc->open = 1;
    177        1.1  augustss 	sc->intr = iintr;
    178        1.1  augustss 	sc->arg = arg;
    179        1.1  augustss 	return 0;
    180        1.1  augustss }
    181        1.1  augustss 
    182  1.15.16.1       mjf static void
    183  1.15.16.1       mjf mpu_close(void *addr)
    184        1.1  augustss {
    185        1.1  augustss 	struct mpu_softc *sc = addr;
    186        1.1  augustss 
    187  1.15.16.1       mjf         DPRINTF(("%s: sc=%p\n", __func__, sc));
    188        1.1  augustss 
    189        1.1  augustss 	sc->open = 0;
    190        1.1  augustss 	sc->intr = 0;
    191        1.1  augustss 	mpu_reset(sc); /* exit UART mode */
    192        1.3     itohy 
    193        1.3     itohy #ifndef AUDIO_NO_POWER_CTL
    194        1.3     itohy 	if (sc->powerctl)
    195        1.3     itohy 		sc->powerctl(sc->powerarg, 0);
    196        1.3     itohy #endif
    197        1.1  augustss }
    198        1.1  augustss 
    199  1.15.16.1       mjf static void
    200  1.15.16.1       mjf mpu_readinput(struct mpu_softc *sc)
    201        1.1  augustss {
    202        1.1  augustss 	bus_space_tag_t iot = sc->iot;
    203        1.1  augustss 	bus_space_handle_t ioh = sc->ioh;
    204        1.1  augustss 	int data;
    205        1.1  augustss 
    206        1.1  augustss 	while(!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY)) {
    207        1.1  augustss 		data = bus_space_read_1(iot, ioh, MPU_DATA);
    208  1.15.16.1       mjf 		DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, sc, data));
    209        1.1  augustss 		if (sc->intr)
    210        1.1  augustss 			sc->intr(sc->arg, data);
    211        1.1  augustss 	}
    212        1.1  augustss }
    213        1.1  augustss 
    214  1.15.16.1       mjf static int
    215  1.15.16.1       mjf mpu_output(void *addr, int d)
    216        1.1  augustss {
    217        1.1  augustss 	struct mpu_softc *sc = addr;
    218        1.1  augustss 	int s;
    219        1.1  augustss 
    220  1.15.16.1       mjf 	DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, sc, d));
    221        1.1  augustss 	if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY)) {
    222        1.1  augustss 		s = splaudio();
    223        1.1  augustss 		mpu_readinput(sc);
    224        1.1  augustss 		splx(s);
    225        1.1  augustss 	}
    226        1.1  augustss 	if (mpu_waitready(sc)) {
    227  1.15.16.1       mjf 		DPRINTF(("%s:: not ready\n", __func__));
    228        1.1  augustss 		return EIO;
    229        1.1  augustss 	}
    230        1.1  augustss 	bus_space_write_1(sc->iot, sc->ioh, MPU_DATA, d);
    231        1.1  augustss 	return 0;
    232        1.1  augustss }
    233        1.1  augustss 
    234  1.15.16.1       mjf static void
    235  1.15.16.1       mjf mpu_getinfo(void *addr, struct midi_info *mi)
    236        1.1  augustss {
    237        1.2  augustss 	struct mpu_softc *sc = addr;
    238        1.1  augustss 
    239        1.2  augustss 	mi->name = sc->model;
    240        1.1  augustss 	mi->props = 0;
    241        1.1  augustss }
    242        1.1  augustss 
    243        1.1  augustss int
    244  1.15.16.1       mjf mpu_intr(void *addr)
    245        1.1  augustss {
    246        1.1  augustss 	struct mpu_softc *sc = addr;
    247        1.1  augustss 
    248        1.1  augustss 	if (MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY) {
    249  1.15.16.1       mjf 		DPRINTF(("%s: no data\n", __func__));
    250  1.15.16.1       mjf 		return 0;
    251        1.1  augustss 	} else {
    252        1.1  augustss 		mpu_readinput(sc);
    253  1.15.16.1       mjf 		return 1;
    254        1.1  augustss 	}
    255        1.1  augustss }
    256