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