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