vr4181aiu.c revision 1.7.8.1 1 1.7.8.1 yamt /* $NetBSD: vr4181aiu.c,v 1.7.8.1 2012/10/30 17:19:45 yamt 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.8.1 yamt __KERNEL_RCSID(0, "$NetBSD: vr4181aiu.c,v 1.7.8.1 2012/10/30 17:19:45 yamt 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 bus_space_tag_t sc_iot;
73 1.1 igy bus_space_handle_t sc_dcu1_ioh;
74 1.1 igy bus_space_handle_t sc_dcu2_ioh;
75 1.1 igy bus_space_handle_t sc_aiu_ioh;
76 1.1 igy u_int16_t *sc_inbuf_head;
77 1.1 igy u_int16_t *sc_inbuf_tail;
78 1.1 igy u_int16_t *sc_inbuf_which;
79 1.1 igy u_int16_t *sc_inbuf1;
80 1.1 igy u_int16_t *sc_inbuf2;
81 1.1 igy u_int16_t *sc_inbuf_raw;
82 1.1 igy int sc_status;
83 1.1 igy };
84 1.1 igy
85 1.7.8.1 yamt static int vr4181aiu_match(device_t, cfdata_t, void *);
86 1.7.8.1 yamt static void vr4181aiu_attach(device_t, device_t, void *);
87 1.1 igy static int vr4181aiu_intr(void *);
88 1.1 igy
89 1.1 igy extern struct cfdriver vr4181aiu_cd;
90 1.1 igy
91 1.7.8.1 yamt CFATTACH_DECL_NEW(vr4181aiu, sizeof(struct vr4181aiu_softc),
92 1.1 igy vr4181aiu_match, vr4181aiu_attach, NULL, NULL);
93 1.1 igy
94 1.1 igy dev_type_open(vr4181aiuopen);
95 1.1 igy dev_type_close(vr4181aiuclose);
96 1.1 igy dev_type_read(vr4181aiuread);
97 1.1 igy dev_type_write(vr4181aiuwrite);
98 1.1 igy
99 1.1 igy const struct cdevsw vr4181aiu_cdevsw = {
100 1.1 igy vr4181aiuopen, vr4181aiuclose, vr4181aiuread, vr4181aiuwrite, noioctl,
101 1.1 igy nostop, notty, nopoll, nommap, nokqfilter,
102 1.1 igy };
103 1.1 igy
104 1.1 igy static int
105 1.7.8.1 yamt vr4181aiu_match(device_t parent, cfdata_t cf, void *aux)
106 1.1 igy {
107 1.1 igy return 1;
108 1.1 igy }
109 1.1 igy
110 1.1 igy static void
111 1.1 igy vr4181aiu_init_inbuf(struct vr4181aiu_softc *sc)
112 1.1 igy {
113 1.1 igy /*
114 1.1 igy * XXXXXXXXXXXXXXXXX
115 1.1 igy *
116 1.1 igy * this is just a quick and dirty hack to locate the buffer
117 1.1 igy * in KSEG0 space. the only reason is that i want the physical
118 1.1 igy * address of the buffer.
119 1.1 igy *
120 1.1 igy * bus_dma framework should be used.
121 1.1 igy */
122 1.1 igy static char inbufbase[INBUF_RAW_SIZE];
123 1.1 igy
124 1.1 igy sc->sc_inbuf_raw = (u_int16_t *) inbufbase;
125 1.1 igy
126 1.1 igy sc->sc_inbuf1 = (u_int16_t *) ((((u_int32_t) sc->sc_inbuf_raw)
127 1.1 igy + INBUF_MASK)
128 1.1 igy & ~INBUF_MASK);
129 1.1 igy sc->sc_inbuf2 = sc->sc_inbuf1 + INBUFLEN;
130 1.1 igy }
131 1.1 igy
132 1.1 igy static void
133 1.1 igy vr4181aiu_disable(struct vr4181aiu_softc *sc)
134 1.1 igy {
135 1.1 igy /* irq clear */
136 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
137 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
138 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
139 1.1 igy VR4181AIU_INT_REG_W,
140 1.1 igy VR4181AIU_MIDLEINTR
141 1.1 igy | VR4181AIU_MSTINTR
142 1.1 igy | VR4181AIU_SIDLEINTR);
143 1.1 igy
144 1.1 igy /* disable microphone */
145 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
146 1.1 igy VR4181AIU_SEQ_REG_W, 0);
147 1.1 igy
148 1.1 igy /* disable ADC */
149 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
150 1.1 igy VR4181AIU_MCNT_REG_W, 0);
151 1.1 igy
152 1.1 igy /* disable DMA */
153 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
154 1.1 igy DCU_AIUDMAMSK_REG_W, 0);
155 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
156 1.1 igy DCU_DMAITMK_REG_W, 0);
157 1.1 igy
158 1.1 igy sc->sc_status = 0;
159 1.1 igy }
160 1.1 igy
161 1.1 igy static void
162 1.7.8.1 yamt vr4181aiu_attach(device_t parent, device_t self, void *aux)
163 1.1 igy {
164 1.1 igy struct vrip_attach_args *va = aux;
165 1.7.8.1 yamt struct vr4181aiu_softc *sc = device_private(self);
166 1.1 igy
167 1.1 igy vr4181aiu_init_inbuf(sc);
168 1.1 igy memset(sc->sc_inbuf1, 0x55, INBUFLEN * 2);
169 1.1 igy memset(sc->sc_inbuf2, 0xaa, INBUFLEN * 2);
170 1.1 igy
171 1.1 igy sc->sc_status = 0;
172 1.1 igy sc->sc_iot = va->va_iot;
173 1.1 igy
174 1.1 igy if (bus_space_map(sc->sc_iot,
175 1.1 igy VR4181AIU_DCU1_BASE, VR4181AIU_DCU1_SIZE,
176 1.1 igy 0, &sc->sc_dcu1_ioh))
177 1.1 igy goto out_dcu1;
178 1.1 igy if (bus_space_map(sc->sc_iot,
179 1.1 igy VR4181AIU_DCU2_BASE, VR4181AIU_DCU2_SIZE,
180 1.1 igy 0, &sc->sc_dcu2_ioh))
181 1.1 igy goto out_dcu2;
182 1.1 igy if (bus_space_map(sc->sc_iot,
183 1.1 igy VR4181AIU_AIU_BASE, VR4181AIU_AIU_SIZE,
184 1.1 igy 0, &sc->sc_aiu_ioh))
185 1.1 igy goto out_aiu;
186 1.1 igy
187 1.1 igy /*
188 1.1 igy * reset AIU
189 1.1 igy */
190 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
191 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIURST);
192 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
193 1.1 igy VR4181AIU_SEQ_REG_W, 0);
194 1.1 igy
195 1.1 igy /*
196 1.1 igy * set sample rate (1kHz fixed)
197 1.1 igy * XXXX
198 1.1 igy * assume to PCLK is 32.768MHz
199 1.1 igy */
200 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
201 1.1 igy VR4181AIU_MCNVC_END,
202 1.1 igy 32768000 / SAMPLEFREQ);
203 1.1 igy
204 1.1 igy /*
205 1.1 igy * XXXX
206 1.1 igy * assume to PCLK is 32.768MHz
207 1.1 igy * DAVREF_SETUP = 5usec * PCLK = 163.84
208 1.1 igy */
209 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
210 1.1 igy VR4181AIU_DAVREF_SETUP_REG_W, 164);
211 1.1 igy
212 1.1 igy vr4181aiu_disable(sc);
213 1.1 igy
214 1.1 igy if (vrip_intr_establish(va->va_vc, va->va_unit, 0,
215 1.1 igy IPL_BIO, vr4181aiu_intr, sc) == NULL) {
216 1.1 igy printf("%s: can't establish interrupt\n",
217 1.7.8.1 yamt device_xname(self));
218 1.1 igy return;
219 1.1 igy }
220 1.1 igy
221 1.1 igy printf("\n");
222 1.1 igy return;
223 1.1 igy
224 1.1 igy out_aiu:
225 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu2_ioh, VR4181AIU_DCU2_SIZE);
226 1.1 igy out_dcu2:
227 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu1_ioh, VR4181AIU_DCU1_SIZE);
228 1.1 igy out_dcu1:
229 1.1 igy printf(": can't map i/o space\n");
230 1.1 igy }
231 1.1 igy
232 1.1 igy int
233 1.4 christos vr4181aiuopen(dev_t dev, int flag, int mode, struct lwp *l)
234 1.1 igy {
235 1.1 igy struct vr4181aiu_softc *sc;
236 1.1 igy
237 1.7 dholland sc = device_lookup_private(&vr4181aiu_cd, minor(dev));
238 1.6 cegger if (sc == NULL)
239 1.1 igy return ENXIO;
240 1.1 igy
241 1.1 igy if (sc->sc_status & ST_BUSY)
242 1.1 igy return EBUSY;
243 1.1 igy
244 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf_tail
245 1.1 igy = sc->sc_inbuf_which = sc->sc_inbuf1;
246 1.1 igy sc->sc_status &= ~ST_OVERRUN;
247 1.1 igy
248 1.1 igy /* setup DMA */
249 1.1 igy /* reset */
250 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
251 1.1 igy DCU_DMARST_REG_W, 0);
252 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
253 1.1 igy DCU_DMARST_REG_W, DCU_DMARST);
254 1.1 igy /* dest1 <- sc_inbuf1 */
255 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
256 1.1 igy DCU_MICDEST1REG1_W,
257 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) & 0xffff);
258 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
259 1.1 igy DCU_MICDEST1REG2_W,
260 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) >> 16);
261 1.1 igy /* dest2 <- sc_inbuf2 */
262 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
263 1.1 igy DCU_MICDEST2REG1_W,
264 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) & 0xffff);
265 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
266 1.1 igy DCU_MICDEST2REG2_W,
267 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) >> 16);
268 1.1 igy /* record length <- INPUTLEN */
269 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
270 1.1 igy DCU_MICRCLEN_REG_W, INPUTLEN);
271 1.1 igy /* config <- auto load */
272 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
273 1.1 igy DCU_MICDMACFG_REG_W, DCU_MICLOAD);
274 1.1 igy /* irq <- irq clear */
275 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
276 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
277 1.1 igy /* control <- INC */
278 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
279 1.1 igy DCU_DMACTL_REG_W, DCU_MICCNT_INC);
280 1.1 igy /* irq mask <- microphone end of process */
281 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
282 1.1 igy DCU_DMAITMK_REG_W, DCU_MICEOP_ENABLE);
283 1.1 igy
284 1.1 igy /* enable DMA */
285 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh,
286 1.1 igy DCU_AIUDMAMSK_REG_W, DCU_ENABLE_MIC);
287 1.1 igy
288 1.1 igy /* enable ADC */
289 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
290 1.1 igy VR4181AIU_MCNT_REG_W, VR4181AIU_ADENAIU);
291 1.1 igy
292 1.1 igy /* enable microphone */
293 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh,
294 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIUMEN);
295 1.1 igy
296 1.1 igy sc->sc_status |= ST_BUSY;
297 1.1 igy
298 1.1 igy return 0;
299 1.1 igy }
300 1.1 igy
301 1.1 igy int
302 1.4 christos vr4181aiuclose(dev_t dev, int flag, int mode, struct lwp *l)
303 1.1 igy {
304 1.6 cegger vr4181aiu_disable(device_lookup_private(&vr4181aiu_cd, minor(dev)));
305 1.1 igy return 0;
306 1.1 igy }
307 1.1 igy
308 1.1 igy int
309 1.1 igy vr4181aiuread(dev_t dev, struct uio *uio, int flag)
310 1.1 igy {
311 1.1 igy struct vr4181aiu_softc *sc;
312 1.1 igy int s;
313 1.1 igy u_int16_t *fence;
314 1.1 igy int avail;
315 1.1 igy int count;
316 1.1 igy u_int8_t tmp[INPUTLEN / PICKUPCOUNT];
317 1.1 igy u_int16_t *src;
318 1.1 igy u_int8_t *dst;
319 1.1 igy
320 1.6 cegger sc = device_lookup_private(&vr4181aiu_cd, minor(dev));
321 1.1 igy
322 1.1 igy src = sc->sc_inbuf_tail;
323 1.1 igy s = splbio();
324 1.1 igy if (src == sc->sc_inbuf_head) {
325 1.1 igy /* wait for DMA to complete writing */
326 1.1 igy tsleep(sc, PRIBIO, "aiu read", 0);
327 1.1 igy /* now sc_inbuf_head points alternate buffer */
328 1.1 igy }
329 1.1 igy splx(s);
330 1.1 igy
331 1.1 igy fence = sc->sc_inbuf_which == sc->sc_inbuf1
332 1.1 igy ? &sc->sc_inbuf1[INPUTLEN]
333 1.1 igy : &sc->sc_inbuf2[INPUTLEN];
334 1.1 igy avail = (fence - src) / PICKUPCOUNT;
335 1.1 igy count = min(avail, uio->uio_resid);
336 1.1 igy dst = tmp;
337 1.1 igy while (count > 0) {
338 1.1 igy *dst++ = (u_int8_t) (*src >> 2);
339 1.1 igy src += PICKUPCOUNT;
340 1.1 igy count--;
341 1.1 igy }
342 1.1 igy
343 1.1 igy if (src < fence) {
344 1.1 igy sc->sc_inbuf_tail = src;
345 1.1 igy } else {
346 1.1 igy /* alter the buffer */
347 1.1 igy sc->sc_inbuf_tail
348 1.1 igy = sc->sc_inbuf_which
349 1.1 igy = sc->sc_inbuf_which == sc->sc_inbuf1
350 1.1 igy ? sc->sc_inbuf2 : sc->sc_inbuf1;
351 1.1 igy }
352 1.1 igy
353 1.1 igy return uiomove(tmp, dst - tmp, uio);
354 1.1 igy }
355 1.1 igy
356 1.1 igy int
357 1.1 igy vr4181aiuwrite(dev_t dev, struct uio *uio, int flag)
358 1.1 igy {
359 1.1 igy return 0;
360 1.1 igy }
361 1.1 igy
362 1.1 igy /*
363 1.1 igy * interrupt handler
364 1.1 igy */
365 1.1 igy static int
366 1.1 igy vr4181aiu_intr(void *arg)
367 1.1 igy {
368 1.1 igy struct vr4181aiu_softc *sc = arg;
369 1.1 igy
370 1.1 igy if (!(sc->sc_status & ST_BUSY)) {
371 1.1 igy printf("vr4181aiu_intr: stray interrupt\n");
372 1.1 igy vr4181aiu_disable(sc);
373 1.1 igy return 0;
374 1.1 igy }
375 1.1 igy
376 1.1 igy /* irq clear */
377 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh,
378 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP);
379 1.1 igy
380 1.1 igy if (sc->sc_inbuf_head == sc->sc_inbuf1) {
381 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf1)
382 1.1 igy sc->sc_status |= ST_OVERRUN;
383 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf2;
384 1.1 igy } else {
385 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf2)
386 1.1 igy sc->sc_status |= ST_OVERRUN;
387 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf1;
388 1.1 igy }
389 1.1 igy
390 1.1 igy if (sc->sc_status & ST_OVERRUN) {
391 1.1 igy printf("vr4181aiu_intr: overrun\n");
392 1.1 igy }
393 1.1 igy
394 1.1 igy DPRINTF(("vr4181aiu_intr: sc_inbuf1 = %04x, sc_inbuf2 = %04x\n",
395 1.1 igy sc->sc_inbuf1[0], sc->sc_inbuf2[0]));
396 1.1 igy
397 1.1 igy wakeup(sc);
398 1.1 igy
399 1.1 igy return 0;
400 1.1 igy }
401