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