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