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