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