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