mpu401.c 5.3 KB

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