vr4181aiu.c revision 1.2 1 1.2 lukem /* $NetBSD: vr4181aiu.c,v 1.2 2003/07/15 02:29:35 lukem Exp $ */
2 1.1 igy
3 1.1 igy /*
4 1.1 igy * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 igy * All rights reserved.
6 1.1 igy *
7 1.1 igy * This code is derived from software contributed to The NetBSD Foundation
8 1.1 igy * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
9 1.1 igy *
10 1.1 igy * Redistribution and use in source and binary forms, with or without
11 1.1 igy * modification, are permitted provided that the following conditions
12 1.1 igy * are met:
13 1.1 igy * 1. Redistributions of source code must retain the above copyright
14 1.1 igy * notice, this list of conditions and the following disclaimer.
15 1.1 igy * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 igy * notice, this list of conditions and the following disclaimer in the
17 1.1 igy * documentation and/or other materials provided with the distribution.
18 1.1 igy * 3. All advertising materials mentioning features or use of this software
19 1.1 igy * must display the following acknowledgement:
20 1.1 igy * This product includes software developed by the NetBSD
21 1.1 igy * Foundation, Inc. and its contributors.
22 1.1 igy * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 igy * contributors may be used to endorse or promote products derived
24 1.1 igy * from this software without specific prior written permission.
25 1.1 igy *
26 1.1 igy * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 igy * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 igy * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 igy * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 igy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 igy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 igy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 igy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 igy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 igy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 igy * POSSIBILITY OF SUCH DAMAGE.
37 1.1 igy */
38 1.2 lukem
39 1.2 lukem #include <sys/cdefs.h>
40 1.2 lukem __KERNEL_RCSID(0, "$NetBSD: vr4181aiu.c,v 1.2 2003/07/15 02:29:35 lukem Exp $");
41 1.1 igy
42 1.1 igy #include <sys/param.h>
43 1.1 igy #include <sys/conf.h>
44 1.1 igy #include <sys/device.h>
45 1.1 igy #include <sys/errno.h>
46 1.1 igy #include <sys/malloc.h>
47 1.1 igy #include <sys/proc.h>
48 1.1 igy #include <sys/systm.h>
49 1.1 igy
50 1.1 igy #include <mips/cpuregs.h>
51 1.1 igy
52 1.1 igy #include <machine/bus.h>
53 1.1 igy
54 1.1 igy #include <hpcmips/vr/vripif.h>
55 1.1 igy #include <hpcmips/vr/vr4181aiureg.h>
56 1.1 igy #include <hpcmips/vr/vr4181dcureg.h>
57 1.1 igy
58 1.1 igy #define INBUFLEN 1024 /* length in u_int16_t */
59 1.1 igy #define INPUTLEN 1000
60 1.1 igy #define SAMPLEFREQ 1000
61 1.1 igy #define PICKUPFREQ 100
62 1.1 igy #define PICKUPCOUNT (SAMPLEFREQ / PICKUPFREQ)
63 1.1 igy
64 1.1 igy #define ST_BUSY 0x01
65 1.1 igy #define ST_OVERRUN 0x02
66 1.1 igy
67 1.1 igy #define INBUF_MASK 0x3ff /* 2Kbyte */
68 1.1 igy #define INBUF_RAW_SIZE (INBUFLEN * 4 + (INBUF_MASK + 1))
69 1.1 igy
70 1.1 igy #ifdef VR4181AIU_DEBUG
71 1.1 igy int vr4181aiu_debug = 0;
72 1.1 igy #define DPRINTF(x) if (vr4181aiu_debug) printf x
73 1.1 igy #else
74 1.1 igy #define DPRINTF(x)
75 1.1 igy #endif
76 1.1 igy
77 1.1 igy
78 1.1 igy struct vr4181aiu_softc {
79 1.1 igy struct device sc_dev;
80 1.1 igy bus_space_tag_t sc_iot;
81 1.1 igy bus_space_handle_t sc_dcu1_ioh;
82 1.1 igy bus_space_handle_t sc_dcu2_ioh;
83 1.1 igy bus_space_handle_t sc_aiu_ioh;
84 1.1 igy u_int16_t *sc_inbuf_head;
85 1.1 igy u_int16_t *sc_inbuf_tail;
86 1.1 igy u_int16_t *sc_inbuf_which;
87 1.1 igy u_int16_t *sc_inbuf1;
88 1.1 igy u_int16_t *sc_inbuf2;
89 1.1 igy u_int16_t *sc_inbuf_raw;
90 1.1 igy int sc_status;
91 1.1 igy };
92 1.1 igy
93 1.1 igy static int vr4181aiu_match(struct device *, struct cfdata *, void *);
94 1.1 igy static void vr4181aiu_attach(struct device *, struct device *, void *);
95 1.1 igy static int vr4181aiu_intr(void *);
96 1.1 igy
97 1.1 igy extern struct cfdriver vr4181aiu_cd;
98 1.1 igy
99 1.1 igy CFATTACH_DECL(vr4181aiu, sizeof(struct vr4181aiu_softc),
100 1.1 igy vr4181aiu_match, vr4181aiu_attach, NULL, NULL);
101 1.1 igy
102 1.1 igy dev_type_open(vr4181aiuopen);
103 1.1 igy dev_type_close(vr4181aiuclose);
104 1.1 igy dev_type_read(vr4181aiuread);
105 1.1 igy dev_type_write(vr4181aiuwrite);
106 1.1 igy
107 1.1 igy const struct cdevsw vr4181aiu_cdevsw = {
108 1.1 igy vr4181aiuopen, vr4181aiuclose, vr4181aiuread, vr4181aiuwrite, noioctl,
109 1.1 igy nostop, notty, nopoll, nommap, nokqfilter,
110 1.1 igy };
111 1.1 igy
112 1.1 igy static int
113 1.1 igy vr4181aiu_match(struct device *parent, struct cfdata *cf, void *aux)
114 1.1 igy {
115 1.1 igy return 1;
116 1.1 igy }
117 1.1 igy
118 1.1 igy static void
119 1.1 igy vr4181aiu_init_inbuf(struct vr4181aiu_softc *sc)
120 1.1 igy {
121 1.1 igy /*
122 1.1 igy * XXXXXXXXXXXXXXXXX
123 1.1 igy *
124 1.1 igy * this is just a quick and dirty hack to locate the buffer
125 1.1 igy * in KSEG0 space. the only reason is that i want the physical
126 1.1 igy * address of the buffer.
127 1.1 igy *
128 1.1 igy * bus_dma framework should be used.
129 1.1 igy */
130 1.1 igy static char inbufbase[INBUF_RAW_SIZE];
131 1.1 igy
132 1.1 igy sc->sc_inbuf_raw = (u_int16_t *) inbufbase;
133 1.1 igy
134 1.1 igy sc->sc_inbuf1 = (u_int16_t *) ((((u_int32_t) sc->sc_inbuf_raw)
135 1.1 igy + INBUF_MASK)
136 1.1 igy & ~INBUF_MASK);
137 1.1 igy sc->sc_inbuf2 = sc->sc_inbuf1 + INBUFLEN;
138 1.1 igy }
139 1.1 igy
140 1.1 igy static void
141 1.1 igy vr4181aiu_disable(struct vr4181aiu_softc *sc)
142 1.1 igy {
143 1.1 igy /* irq clear */
144 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
145 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
146 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
147 1.1 igy VR4181AIU_INT_REG_W,
148 1.1 igy VR4181AIU_MIDLEINTR
149 1.1 igy | VR4181AIU_MSTINTR
150 1.1 igy | VR4181AIU_SIDLEINTR);
151 1.1 igy
152 1.1 igy /* disable microphone */
153 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
154 1.1 igy VR4181AIU_SEQ_REG_W, 0);
155 1.1 igy
156 1.1 igy /* disable ADC */
157 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
158 1.1 igy VR4181AIU_MCNT_REG_W, 0);
159 1.1 igy
160 1.1 igy /* disable DMA */
161 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
162 1.1 igy DCU_AIUDMAMSK_REG_W, 0);
163 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
164 1.1 igy DCU_DMAITMK_REG_W, 0);
165 1.1 igy
166 1.1 igy sc->sc_status = 0;
167 1.1 igy }
168 1.1 igy
169 1.1 igy static void
170 1.1 igy vr4181aiu_attach(struct device *parent, struct device *self, void *aux)
171 1.1 igy {
172 1.1 igy struct vrip_attach_args *va = aux;
173 1.1 igy struct vr4181aiu_softc *sc = (void *) self;
174 1.1 igy
175 1.1 igy vr4181aiu_init_inbuf(sc);
176 1.1 igy memset(sc->sc_inbuf1, 0x55, INBUFLEN * 2);
177 1.1 igy memset(sc->sc_inbuf2, 0xaa, INBUFLEN * 2);
178 1.1 igy
179 1.1 igy sc->sc_status = 0;
180 1.1 igy sc->sc_iot = va->va_iot;
181 1.1 igy
182 1.1 igy if (bus_space_map(sc->sc_iot,
183 1.1 igy VR4181AIU_DCU1_BASE, VR4181AIU_DCU1_SIZE,
184 1.1 igy 0, &sc->sc_dcu1_ioh))
185 1.1 igy goto out_dcu1;
186 1.1 igy if (bus_space_map(sc->sc_iot,
187 1.1 igy VR4181AIU_DCU2_BASE, VR4181AIU_DCU2_SIZE,
188 1.1 igy 0, &sc->sc_dcu2_ioh))
189 1.1 igy goto out_dcu2;
190 1.1 igy if (bus_space_map(sc->sc_iot,
191 1.1 igy VR4181AIU_AIU_BASE, VR4181AIU_AIU_SIZE,
192 1.1 igy 0, &sc->sc_aiu_ioh))
193 1.1 igy goto out_aiu;
194 1.1 igy
195 1.1 igy /*
196 1.1 igy * reset AIU
197 1.1 igy */
198 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
199 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIURST);
200 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
201 1.1 igy VR4181AIU_SEQ_REG_W, 0);
202 1.1 igy
203 1.1 igy /*
204 1.1 igy * set sample rate (1kHz fixed)
205 1.1 igy * XXXX
206 1.1 igy * assume to PCLK is 32.768MHz
207 1.1 igy */
208 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
209 1.1 igy VR4181AIU_MCNVC_END,
210 1.1 igy 32768000 / SAMPLEFREQ);
211 1.1 igy
212 1.1 igy /*
213 1.1 igy * XXXX
214 1.1 igy * assume to PCLK is 32.768MHz
215 1.1 igy * DAVREF_SETUP = 5usec * PCLK = 163.84
216 1.1 igy */
217 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
218 1.1 igy VR4181AIU_DAVREF_SETUP_REG_W, 164);
219 1.1 igy
220 1.1 igy vr4181aiu_disable(sc);
221 1.1 igy
222 1.1 igy if (vrip_intr_establish(va->va_vc, va->va_unit, 0,
223 1.1 igy IPL_BIO, vr4181aiu_intr, sc) == NULL) {
224 1.1 igy printf("%s: can't establish interrupt\n",
225 1.1 igy sc->sc_dev.dv_xname);
226 1.1 igy return;
227 1.1 igy }
228 1.1 igy
229 1.1 igy printf("\n");
230 1.1 igy return;
231 1.1 igy
232 1.1 igy out_aiu:
233 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu2_ioh, VR4181AIU_DCU2_SIZE);
234 1.1 igy out_dcu2:
235 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu1_ioh, VR4181AIU_DCU1_SIZE);
236 1.1 igy out_dcu1:
237 1.1 igy printf(": can't map i/o space\n");
238 1.1 igy }
239 1.1 igy
240 1.1 igy int
241 1.1 igy vr4181aiuopen(dev_t dev, int flag, int mode, struct proc *p)
242 1.1 igy {
243 1.1 igy struct vr4181aiu_softc *sc;
244 1.1 igy
245 1.1 igy if ((sc = device_lookup(&vr4181aiu_cd, minor(dev))) == NULL)
246 1.1 igy return ENXIO;
247 1.1 igy
248 1.1 igy if (sc->sc_status & ST_BUSY)
249 1.1 igy return EBUSY;
250 1.1 igy
251 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf_tail
252 1.1 igy = sc->sc_inbuf_which = sc->sc_inbuf1;
253 1.1 igy sc->sc_status &= ~ST_OVERRUN;
254 1.1 igy
255 1.1 igy /* setup DMA */
256 1.1 igy /* reset */
257 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
258 1.1 igy DCU_DMARST_REG_W, 0);
259 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
260 1.1 igy DCU_DMARST_REG_W, DCU_DMARST);
261 1.1 igy /* dest1 <- sc_inbuf1 */
262 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
263 1.1 igy DCU_MICDEST1REG1_W,
264 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) & 0xffff);
265 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
266 1.1 igy DCU_MICDEST1REG2_W,
267 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) >> 16);
268 1.1 igy /* dest2 <- sc_inbuf2 */
269 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
270 1.1 igy DCU_MICDEST2REG1_W,
271 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) & 0xffff);
272 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
273 1.1 igy DCU_MICDEST2REG2_W,
274 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) >> 16);
275 1.1 igy /* record length <- INPUTLEN */
276 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
277 1.1 igy DCU_MICRCLEN_REG_W, INPUTLEN);
278 1.1 igy /* config <- auto load */
279 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
280 1.1 igy DCU_MICDMACFG_REG_W, DCU_MICLOAD);
281 1.1 igy /* irq <- irq clear */
282 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
283 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
284 1.1 igy /* control <- INC */
285 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
286 1.1 igy DCU_DMACTL_REG_W, DCU_MICCNT_INC);
287 1.1 igy /* irq mask <- microphone end of process */
288 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
289 1.1 igy DCU_DMAITMK_REG_W, DCU_MICEOP_ENABLE);
290 1.1 igy
291 1.1 igy /* enable DMA */
292 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
293 1.1 igy DCU_AIUDMAMSK_REG_W, DCU_ENABLE_MIC);
294 1.1 igy
295 1.1 igy /* enable ADC */
296 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
297 1.1 igy VR4181AIU_MCNT_REG_W, VR4181AIU_ADENAIU);
298 1.1 igy
299 1.1 igy /* enable microphone */
300 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
301 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIUMEN);
302 1.1 igy
303 1.1 igy sc->sc_status |= ST_BUSY;
304 1.1 igy
305 1.1 igy return 0;
306 1.1 igy }
307 1.1 igy
308 1.1 igy int
309 1.1 igy vr4181aiuclose(dev_t dev, int flag, int mode, struct proc *p)
310 1.1 igy {
311 1.1 igy vr4181aiu_disable(device_lookup(&vr4181aiu_cd, minor(dev)));
312 1.1 igy return 0;
313 1.1 igy }
314 1.1 igy
315 1.1 igy int
316 1.1 igy vr4181aiuread(dev_t dev, struct uio *uio, int flag)
317 1.1 igy {
318 1.1 igy struct vr4181aiu_softc *sc;
319 1.1 igy int s;
320 1.1 igy u_int16_t *fence;
321 1.1 igy int avail;
322 1.1 igy int count;
323 1.1 igy u_int8_t tmp[INPUTLEN / PICKUPCOUNT];
324 1.1 igy u_int16_t *src;
325 1.1 igy u_int8_t *dst;
326 1.1 igy
327 1.1 igy sc = device_lookup(&vr4181aiu_cd, minor(dev));
328 1.1 igy
329 1.1 igy src = sc->sc_inbuf_tail;
330 1.1 igy s = splbio();
331 1.1 igy if (src == sc->sc_inbuf_head) {
332 1.1 igy /* wait for DMA to complete writing */
333 1.1 igy tsleep(sc, PRIBIO, "aiu read", 0);
334 1.1 igy /* now sc_inbuf_head points alternate buffer */
335 1.1 igy }
336 1.1 igy splx(s);
337 1.1 igy
338 1.1 igy fence = sc->sc_inbuf_which == sc->sc_inbuf1
339 1.1 igy ? &sc->sc_inbuf1[INPUTLEN]
340 1.1 igy : &sc->sc_inbuf2[INPUTLEN];
341 1.1 igy avail = (fence - src) / PICKUPCOUNT;
342 1.1 igy count = min(avail, uio->uio_resid);
343 1.1 igy dst = tmp;
344 1.1 igy while (count > 0) {
345 1.1 igy *dst++ = (u_int8_t) (*src >> 2);
346 1.1 igy src += PICKUPCOUNT;
347 1.1 igy count--;
348 1.1 igy }
349 1.1 igy
350 1.1 igy if (src < fence) {
351 1.1 igy sc->sc_inbuf_tail = src;
352 1.1 igy } else {
353 1.1 igy /* alter the buffer */
354 1.1 igy sc->sc_inbuf_tail
355 1.1 igy = sc->sc_inbuf_which
356 1.1 igy = sc->sc_inbuf_which == sc->sc_inbuf1
357 1.1 igy ? sc->sc_inbuf2 : sc->sc_inbuf1;
358 1.1 igy }
359 1.1 igy
360 1.1 igy return uiomove(tmp, dst - tmp, uio);
361 1.1 igy }
362 1.1 igy
363 1.1 igy int
364 1.1 igy vr4181aiuwrite(dev_t dev, struct uio *uio, int flag)
365 1.1 igy {
366 1.1 igy return 0;
367 1.1 igy }
368 1.1 igy
369 1.1 igy int
370 1.1 igy vr4181aiupoll(dev_t dev, int events, struct proc *p)
371 1.1 igy {
372 1.1 igy return 0;
373 1.1 igy }
374 1.1 igy
375 1.1 igy int
376 1.1 igy vr4181aiuioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
377 1.1 igy {
378 1.1 igy return 0;
379 1.1 igy }
380 1.1 igy
381 1.1 igy /*
382 1.1 igy * interrupt handler
383 1.1 igy */
384 1.1 igy static int
385 1.1 igy vr4181aiu_intr(void *arg)
386 1.1 igy {
387 1.1 igy struct vr4181aiu_softc *sc = arg;
388 1.1 igy
389 1.1 igy if (!(sc->sc_status & ST_BUSY)) {
390 1.1 igy printf("vr4181aiu_intr: stray interrupt\n");
391 1.1 igy vr4181aiu_disable(sc);
392 1.1 igy return 0;
393 1.1 igy }
394 1.1 igy
395 1.1 igy /* irq clear */
396 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
397 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
398 1.1 igy
399 1.1 igy if (sc->sc_inbuf_head == sc->sc_inbuf1) {
400 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf1)
401 1.1 igy sc->sc_status |= ST_OVERRUN;
402 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf2;
403 1.1 igy } else {
404 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf2)
405 1.1 igy sc->sc_status |= ST_OVERRUN;
406 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf1;
407 1.1 igy }
408 1.1 igy
409 1.1 igy if (sc->sc_status & ST_OVERRUN) {
410 1.1 igy printf("vr4181aiu_intr: overrun\n");
411 1.1 igy }
412 1.1 igy
413 1.1 igy DPRINTF(("vr4181aiu_intr: sc_inbuf1 = %04x, sc_inbuf2 = %04x\n",
414 1.1 igy sc->sc_inbuf1[0], sc->sc_inbuf2[0]));
415 1.1 igy
416 1.1 igy wakeup(sc);
417 1.1 igy
418 1.1 igy return 0;
419 1.1 igy }
420