mpu.c revision 1.18 1 1.18 jmcneill /* $NetBSD: mpu.c,v 1.18 2011/11/23 23:07:32 jmcneill Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.18 jmcneill * Copyright (c) 1998, 2008 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.18 jmcneill * by Lennart Augustsson (augustss (at) NetBSD.org) and by Andrew Doran.
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 *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.18 jmcneill __KERNEL_RCSID(0, "$NetBSD: mpu.c,v 1.18 2011/11/23 23:07:32 jmcneill Exp $");
34 1.1 augustss
35 1.1 augustss #include <sys/param.h>
36 1.1 augustss #include <sys/systm.h>
37 1.1 augustss #include <sys/errno.h>
38 1.1 augustss #include <sys/ioctl.h>
39 1.1 augustss #include <sys/syslog.h>
40 1.1 augustss #include <sys/device.h>
41 1.1 augustss #include <sys/proc.h>
42 1.1 augustss #include <sys/buf.h>
43 1.1 augustss
44 1.15 ad #include <sys/cpu.h>
45 1.15 ad #include <sys/intr.h>
46 1.15 ad #include <sys/bus.h>
47 1.1 augustss
48 1.1 augustss #include <dev/midi_if.h>
49 1.1 augustss
50 1.1 augustss #include <dev/ic/mpuvar.h>
51 1.1 augustss
52 1.1 augustss #ifdef AUDIO_DEBUG
53 1.1 augustss #define DPRINTF(x) if (mpudebug) printf x
54 1.1 augustss #define DPRINTFN(n,x) if (mpudebug >= (n)) printf x
55 1.1 augustss int mpudebug = 0;
56 1.1 augustss #else
57 1.1 augustss #define DPRINTF(x)
58 1.1 augustss #define DPRINTFN(n,x)
59 1.1 augustss #endif
60 1.1 augustss
61 1.1 augustss #define MPU_DATA 0
62 1.1 augustss #define MPU_COMMAND 1
63 1.1 augustss #define MPU_RESET 0xff
64 1.1 augustss #define MPU_UART_MODE 0x3f
65 1.1 augustss #define MPU_ACK 0xfe
66 1.1 augustss #define MPU_STATUS 1
67 1.1 augustss #define MPU_OUTPUT_BUSY 0x40
68 1.1 augustss #define MPU_INPUT_EMPTY 0x80
69 1.1 augustss
70 1.1 augustss #define MPU_MAXWAIT 10000 /* usec/10 to wait */
71 1.1 augustss
72 1.1 augustss #define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
73 1.1 augustss
74 1.16 xtraeme static int mpu_reset(struct mpu_softc *);
75 1.12 perry static inline int mpu_waitready(struct mpu_softc *);
76 1.16 xtraeme static void mpu_readinput(struct mpu_softc *);
77 1.1 augustss
78 1.16 xtraeme static int mpu_open(void *, int,
79 1.9 perry void (*iintr)(void *, int),
80 1.9 perry void (*ointr)(void *), void *arg);
81 1.16 xtraeme static void mpu_close(void *);
82 1.16 xtraeme static int mpu_output(void *, int);
83 1.16 xtraeme static void mpu_getinfo(void *, struct midi_info *);
84 1.18 jmcneill static void mpu_get_locks(void *, kmutex_t **, kmutex_t **);
85 1.1 augustss
86 1.7 yamt const struct midi_hw_if mpu_midi_hw_if = {
87 1.1 augustss mpu_open,
88 1.1 augustss mpu_close,
89 1.1 augustss mpu_output,
90 1.1 augustss mpu_getinfo,
91 1.1 augustss 0, /* ioctl */
92 1.18 jmcneill mpu_get_locks,
93 1.1 augustss };
94 1.1 augustss
95 1.1 augustss int
96 1.16 xtraeme mpu_find(struct mpu_softc *sc)
97 1.1 augustss {
98 1.1 augustss if (MPU_GETSTATUS(sc->iot, sc->ioh) == 0xff) {
99 1.16 xtraeme DPRINTF(("%s: No status\n", __func__));
100 1.1 augustss goto bad;
101 1.1 augustss }
102 1.1 augustss sc->open = 0;
103 1.1 augustss sc->intr = 0;
104 1.1 augustss if (mpu_reset(sc) == 0)
105 1.1 augustss return 1;
106 1.1 augustss bad:
107 1.1 augustss return 0;
108 1.1 augustss }
109 1.1 augustss
110 1.2 augustss void
111 1.16 xtraeme mpu_attach(struct mpu_softc *sc)
112 1.2 augustss {
113 1.18 jmcneill
114 1.18 jmcneill if (sc->lock == NULL) {
115 1.18 jmcneill panic("mpu_attach: no lock");
116 1.18 jmcneill }
117 1.18 jmcneill
118 1.16 xtraeme midi_attach_mi(&mpu_midi_hw_if, sc, sc->sc_dev);
119 1.2 augustss }
120 1.2 augustss
121 1.12 perry static inline int
122 1.16 xtraeme mpu_waitready(struct mpu_softc *sc)
123 1.1 augustss {
124 1.1 augustss int i;
125 1.1 augustss
126 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
127 1.18 jmcneill
128 1.1 augustss for(i = 0; i < MPU_MAXWAIT; i++) {
129 1.1 augustss if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_OUTPUT_BUSY))
130 1.1 augustss return 0;
131 1.1 augustss delay(10);
132 1.1 augustss }
133 1.1 augustss return 1;
134 1.1 augustss }
135 1.1 augustss
136 1.16 xtraeme static int
137 1.16 xtraeme mpu_reset(struct mpu_softc *sc)
138 1.1 augustss {
139 1.1 augustss bus_space_tag_t iot = sc->iot;
140 1.1 augustss bus_space_handle_t ioh = sc->ioh;
141 1.1 augustss int i;
142 1.18 jmcneill
143 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
144 1.1 augustss
145 1.1 augustss if (mpu_waitready(sc)) {
146 1.16 xtraeme DPRINTF(("%s: not ready\n", __func__));
147 1.1 augustss return EIO;
148 1.1 augustss }
149 1.1 augustss bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
150 1.1 augustss for(i = 0; i < 2*MPU_MAXWAIT; i++) {
151 1.1 augustss if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
152 1.1 augustss bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
153 1.1 augustss return 0;
154 1.1 augustss }
155 1.1 augustss }
156 1.16 xtraeme DPRINTF(("%s: No ACK\n", __func__));
157 1.1 augustss return EIO;
158 1.1 augustss }
159 1.1 augustss
160 1.16 xtraeme static int
161 1.14 christos mpu_open(void *addr, int flags, void (*iintr)(void *, int),
162 1.14 christos void (*ointr)(void *), void *arg)
163 1.1 augustss {
164 1.1 augustss struct mpu_softc *sc = addr;
165 1.1 augustss
166 1.16 xtraeme DPRINTF(("%s: sc=%p\n", __func__, sc));
167 1.1 augustss
168 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
169 1.18 jmcneill
170 1.1 augustss if (sc->open)
171 1.1 augustss return EBUSY;
172 1.3 itohy #ifndef AUDIO_NO_POWER_CTL
173 1.3 itohy if (sc->powerctl)
174 1.3 itohy sc->powerctl(sc->powerarg, 1);
175 1.3 itohy #endif
176 1.3 itohy if (mpu_reset(sc) != 0) {
177 1.3 itohy #ifndef AUDIO_NO_POWER_CTL
178 1.3 itohy if (sc->powerctl)
179 1.3 itohy sc->powerctl(sc->powerarg, 0);
180 1.3 itohy #endif
181 1.1 augustss return EIO;
182 1.3 itohy }
183 1.1 augustss
184 1.1 augustss bus_space_write_1(sc->iot, sc->ioh, MPU_COMMAND, MPU_UART_MODE);
185 1.1 augustss sc->open = 1;
186 1.1 augustss sc->intr = iintr;
187 1.1 augustss sc->arg = arg;
188 1.1 augustss return 0;
189 1.1 augustss }
190 1.1 augustss
191 1.16 xtraeme static void
192 1.16 xtraeme mpu_close(void *addr)
193 1.1 augustss {
194 1.1 augustss struct mpu_softc *sc = addr;
195 1.1 augustss
196 1.16 xtraeme DPRINTF(("%s: sc=%p\n", __func__, sc));
197 1.1 augustss
198 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
199 1.18 jmcneill
200 1.1 augustss sc->open = 0;
201 1.1 augustss sc->intr = 0;
202 1.1 augustss mpu_reset(sc); /* exit UART mode */
203 1.3 itohy
204 1.3 itohy #ifndef AUDIO_NO_POWER_CTL
205 1.3 itohy if (sc->powerctl)
206 1.3 itohy sc->powerctl(sc->powerarg, 0);
207 1.3 itohy #endif
208 1.1 augustss }
209 1.1 augustss
210 1.16 xtraeme static void
211 1.16 xtraeme mpu_readinput(struct mpu_softc *sc)
212 1.1 augustss {
213 1.1 augustss bus_space_tag_t iot = sc->iot;
214 1.1 augustss bus_space_handle_t ioh = sc->ioh;
215 1.1 augustss int data;
216 1.1 augustss
217 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
218 1.18 jmcneill
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.16 xtraeme DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, 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.16 xtraeme static int
228 1.16 xtraeme mpu_output(void *addr, int d)
229 1.1 augustss {
230 1.1 augustss struct mpu_softc *sc = addr;
231 1.18 jmcneill
232 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
233 1.1 augustss
234 1.16 xtraeme DPRINTFN(3, ("%s: sc=%p 0x%02x\n", __func__, sc, d));
235 1.1 augustss if (!(MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY)) {
236 1.1 augustss mpu_readinput(sc);
237 1.1 augustss }
238 1.1 augustss if (mpu_waitready(sc)) {
239 1.16 xtraeme DPRINTF(("%s:: not ready\n", __func__));
240 1.1 augustss return EIO;
241 1.1 augustss }
242 1.1 augustss bus_space_write_1(sc->iot, sc->ioh, MPU_DATA, d);
243 1.1 augustss return 0;
244 1.1 augustss }
245 1.1 augustss
246 1.16 xtraeme static void
247 1.16 xtraeme mpu_getinfo(void *addr, struct midi_info *mi)
248 1.1 augustss {
249 1.2 augustss struct mpu_softc *sc = addr;
250 1.1 augustss
251 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
252 1.18 jmcneill
253 1.2 augustss mi->name = sc->model;
254 1.1 augustss mi->props = 0;
255 1.1 augustss }
256 1.1 augustss
257 1.18 jmcneill static void
258 1.18 jmcneill mpu_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
259 1.18 jmcneill {
260 1.18 jmcneill struct mpu_softc *sc = addr;
261 1.18 jmcneill
262 1.18 jmcneill *intr = sc->lock;
263 1.18 jmcneill *proc = NULL;
264 1.18 jmcneill }
265 1.18 jmcneill
266 1.1 augustss int
267 1.16 xtraeme mpu_intr(void *addr)
268 1.1 augustss {
269 1.1 augustss struct mpu_softc *sc = addr;
270 1.1 augustss
271 1.18 jmcneill KASSERT(mutex_owned(sc->lock));
272 1.18 jmcneill
273 1.1 augustss if (MPU_GETSTATUS(sc->iot, sc->ioh) & MPU_INPUT_EMPTY) {
274 1.16 xtraeme DPRINTF(("%s: no data\n", __func__));
275 1.16 xtraeme return 0;
276 1.1 augustss } else {
277 1.1 augustss mpu_readinput(sc);
278 1.16 xtraeme return 1;
279 1.1 augustss }
280 1.1 augustss }
281