vidcaudio.c revision 1.40.2.3 1 /* $NetBSD: vidcaudio.c,v 1.40.2.3 2005/01/09 08:42:44 kent Exp $ */
2
3 /*
4 * Copyright (c) 1995 Melvin Tang-Richardson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the RiscBSD team.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 2003 Ben Harris
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59 /*
60 * audio driver for the RiscPC 8/16 bit sound
61 *
62 * Interfaces with the NetBSD generic audio driver to provide SUN
63 * /dev/audio (partial) compatibility.
64 */
65
66 #include <sys/param.h> /* proc.h */
67
68 __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.40.2.3 2005/01/09 08:42:44 kent Exp $");
69
70 #include <sys/audioio.h>
71 #include <sys/conf.h> /* autoconfig functions */
72 #include <sys/device.h> /* device calls */
73 #include <sys/errno.h>
74 #include <sys/malloc.h>
75 #include <sys/proc.h> /* device calls */
76 #include <sys/systm.h>
77
78 #include <uvm/uvm_extern.h>
79
80 #include <dev/audio_if.h>
81 #include <dev/audiobellvar.h>
82 #include <dev/auconv.h>
83 #include <dev/mulaw.h>
84
85 #include <machine/intr.h>
86 #include <machine/machdep.h>
87 #include <arm/arm32/katelib.h>
88
89 #include <arm/iomd/vidcaudiovar.h>
90 #include <arm/iomd/iomdreg.h>
91 #include <arm/iomd/iomdvar.h>
92 #include <arm/iomd/vidc.h>
93 #include <arm/mainbus/mainbus.h>
94
95 #include "pckbd.h"
96 #if NPCKBD > 0
97 #include <dev/pckbport/pckbdvar.h>
98 #endif
99
100 #include "rpckbd.h"
101 #if NRPCKBD > 0
102 #include <arm/iomd/rpckbdvar.h>
103 #endif
104
105 #include "vt.h"
106 #if NVT > 0
107 extern void vt_hookup_bell(void (*)(void *, u_int, u_int, u_int, int), void *);
108 #endif
109
110 extern int *vidc_base;
111
112 #ifdef VIDCAUDIO_DEBUG
113 #define DPRINTF(x) printf x
114 #else
115 #define DPRINTF(x)
116 #endif
117
118 struct vidcaudio_softc {
119 struct device sc_dev;
120
121 irqhandler_t sc_ih;
122 int sc_dma_intr;
123
124 int sc_is16bit;
125
126 size_t sc_pblksize;
127 vm_offset_t sc_poffset;
128 vm_offset_t sc_pbufsize;
129 paddr_t *sc_ppages;
130 void (*sc_pintr)(void *);
131 void *sc_parg;
132 int sc_pcountdown;
133 };
134
135 static int vidcaudio_probe(struct device *, struct cfdata *, void *);
136 static void vidcaudio_attach(struct device *, struct device *, void *);
137 static void vidcaudio_close(void *);
138
139 static int vidcaudio_intr(void *);
140 static void vidcaudio_rate(int);
141 static void vidcaudio_ctrl(int);
142 static void vidcaudio_stereo(int, int);
143 static stream_filter_factory_t mulaw_to_vidc;
144 static stream_filter_factory_t mulaw_to_vidc_stereo;
145 static int mulaw_to_vidc_fetch_to(stream_fetcher_t *, audio_stream_t *, int);
146 static int mulaw_to_vidc_stereo_fetch_to(stream_fetcher_t *,
147 audio_stream_t *, int);
148
149 CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
150 vidcaudio_probe, vidcaudio_attach, NULL, NULL);
151
152 static int vidcaudio_query_encoding(void *, struct audio_encoding *);
153 static int vidcaudio_set_params(void *, int, int, audio_params_t *,
154 audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
155 static int vidcaudio_round_blocksize(void *, int, int, const audio_params_t *);
156 static int vidcaudio_trigger_output(void *, void *, void *, int,
157 void (*)(void *), void *, const audio_params_t *);
158 static int vidcaudio_trigger_input(void *, void *, void *, int,
159 void (*)(void *), void *, const audio_params_t *);
160 static int vidcaudio_halt_output(void *);
161 static int vidcaudio_halt_input(void *);
162 static int vidcaudio_getdev(void *, struct audio_device *);
163 static int vidcaudio_set_port(void *, mixer_ctrl_t *);
164 static int vidcaudio_get_port(void *, mixer_ctrl_t *);
165 static int vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
166 static int vidcaudio_get_props(void *);
167
168 static struct audio_device vidcaudio_device = {
169 "ARM VIDC",
170 "",
171 "vidcaudio"
172 };
173
174 static const struct audio_hw_if vidcaudio_hw_if = {
175 NULL, /* open */
176 vidcaudio_close,
177 NULL,
178 vidcaudio_query_encoding,
179 vidcaudio_set_params,
180 vidcaudio_round_blocksize,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 NULL,
186 vidcaudio_halt_output,
187 vidcaudio_halt_input,
188 NULL,
189 vidcaudio_getdev,
190 NULL,
191 vidcaudio_set_port,
192 vidcaudio_get_port,
193 vidcaudio_query_devinfo,
194 NULL,
195 NULL,
196 NULL,
197 NULL,
198 vidcaudio_get_props,
199 vidcaudio_trigger_output,
200 vidcaudio_trigger_input,
201 NULL,
202 };
203
204 static int
205 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
206 {
207 int id;
208
209 id = IOMD_ID;
210
211 /* So far I only know about this IOMD */
212 switch (id) {
213 case RPC600_IOMD_ID:
214 case ARM7500_IOC_ID:
215 case ARM7500FE_IOC_ID:
216 return 1;
217 default:
218 aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
219 return 0;
220 }
221 }
222
223
224 static void
225 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
226 {
227 struct vidcaudio_softc *sc = (void *)self;
228 struct device *beepdev;
229
230 switch (IOMD_ID) {
231 #ifndef EB7500ATX
232 case RPC600_IOMD_ID:
233 sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
234 sc->sc_dma_intr = IRQ_DMASCH0;
235 break;
236 #endif
237 case ARM7500_IOC_ID:
238 case ARM7500FE_IOC_ID:
239 sc->sc_is16bit = TRUE;
240 sc->sc_dma_intr = IRQ_SDMA;
241 break;
242 default:
243 aprint_error(": strange IOMD\n");
244 return;
245 }
246
247 if (sc->sc_is16bit)
248 aprint_normal(": 16-bit external DAC\n");
249 else
250 aprint_normal(": 8-bit internal DAC\n");
251
252 /* Install the irq handler for the DMA interrupt */
253 sc->sc_ih.ih_func = vidcaudio_intr;
254 sc->sc_ih.ih_arg = sc;
255 sc->sc_ih.ih_level = IPL_AUDIO;
256 sc->sc_ih.ih_name = self->dv_xname;
257
258 if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
259 aprint_error("%s: couldn't claim IRQ %d\n",
260 self->dv_xname, sc->sc_dma_intr);
261 return;
262 }
263
264 disable_irq(sc->sc_dma_intr);
265
266 beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
267 #if NPCKBD > 0
268 pckbd_hookup_bell(audiobell, beepdev);
269 #endif
270 #if NRPCKBD > 0
271 rpckbd_hookup_bell(audiobell, beepdev);
272 #endif
273 #if NVT > 0
274 vt_hookup_bell(audiobell, beepdev);
275 #endif
276 }
277
278 static void
279 vidcaudio_close(void *addr)
280 {
281 struct vidcaudio_softc *sc = addr;
282
283 DPRINTF(("DEBUG: vidcaudio_close called\n"));
284 /*
285 * We do this here rather than in vidcaudio_halt_output()
286 * because the latter can be called from interrupt context
287 * (audio_pint()->audio_clear()->vidcaudio_halt_output()).
288 */
289 if (sc->sc_ppages != NULL) {
290 free(sc->sc_ppages, M_DEVBUF);
291 sc->sc_ppages = NULL;
292 }
293 }
294
295 /*
296 * Interface to the generic audio driver
297 */
298
299 static int
300 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
301 {
302 struct vidcaudio_softc *sc = addr;
303
304 switch (fp->index) {
305 case 0:
306 strcpy(fp->name, AudioEmulaw);
307 fp->encoding = AUDIO_ENCODING_ULAW;
308 fp->precision = 8;
309 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
310 break;
311
312 case 1:
313 if (sc->sc_is16bit) {
314 strcpy(fp->name, AudioEslinear_le);
315 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
316 fp->precision = 16;
317 fp->flags = 0;
318 break;
319 }
320 /* FALLTHROUGH */
321 default:
322 return EINVAL;
323 }
324 return 0;
325 }
326
327 #define MULAW_TO_VIDC(m) (~((m) << 1 | (m) >> 7))
328
329 static stream_filter_t *
330 mulaw_to_vidc(struct audio_softc *sc, const audio_params_t *from,
331 const audio_params_t *to)
332 {
333 return auconv_nocontext_filter_factory(mulaw_to_vidc_fetch_to);
334 }
335
336 static int
337 mulaw_to_vidc_fetch_to(stream_fetcher_t *self, audio_stream_t *dst, int max_used)
338 {
339 stream_filter_t *this;
340 int m, err;
341
342 this = (stream_filter_t *)self;
343 if ((err = this->prev->fetch_to(this->prev, this->src, max_used)))
344 return err;
345 m = dst->end - dst->start;
346 m = min(m, max_used);
347 FILTER_LOOP_PROLOGUE(this->src, 1, dst, 1, m) {
348 *d = MULAW_TO_VIDC(*s);
349 } FILTER_LOOP_EPILOGUE(this->src, dst);
350 return 0;
351 }
352
353 static stream_filter_t *
354 mulaw_to_vidc_stereo(struct audio_softc *sc, const audio_params_t *from,
355 const audio_params_t *to)
356 {
357 return auconv_nocontext_filter_factory(mulaw_to_vidc_stereo_fetch_to);
358 }
359
360 static int
361 mulaw_to_vidc_stereo_fetch_to(stream_fetcher_t *self, audio_stream_t *dst,
362 int max_used)
363 {
364 stream_filter_t *this;
365 int m, err;
366
367 this = (stream_filter_t *)self;
368 max_used = (max_used + 1) & ~1;
369 if ((err = this->prev->fetch_to(this->prev, this->src, max_used / 2)))
370 return err;
371 m = (dst->end - dst->start) & ~1;
372 m = min(m, max_used);
373 FILTER_LOOP_PROLOGUE(this->src, 1, dst, 2, m) {
374 d[0] = d[1] = MULAW_TO_VIDC(*s);
375 } FILTER_LOOP_EPILOGUE(this->src, dst);
376 return 0;
377 }
378
379 static int
380 vidcaudio_set_params(void *addr, int setmode, int usemode,
381 audio_params_t *p, audio_params_t *r,
382 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
383 {
384 audio_params_t hw;
385 struct vidcaudio_softc *sc = addr;
386 int sample_period, ch;
387
388 if ((setmode & AUMODE_PLAY) == 0)
389 return 0;
390
391 if (sc->sc_is16bit) {
392 /* ARM7500ish, 16-bit, two-channel */
393 hw = *p;
394 if (p->encoding == AUDIO_ENCODING_ULAW && p->precision == 8) {
395 hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
396 hw.precision = hw.validbits = 16;
397 pfil->append(pfil, mulaw_to_linear16, &hw);
398 } else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
399 p->precision != 16)
400 return EINVAL;
401 sample_period = 705600 / 4 / p->sample_rate;
402 if (sample_period < 3) sample_period = 3;
403 vidcaudio_rate(sample_period - 2);
404 vidcaudio_ctrl(SCR_SERIAL);
405 hw.sample_rate = 705600 / 4 / sample_period;
406 hw.channels = 2;
407 pfil->append(pfil, aurateconv, &hw);
408 } else {
409 /* VIDC20ish, u-law, 8-channel */
410 if (p->encoding != AUDIO_ENCODING_ULAW || p->precision != 8)
411 return EINVAL;
412 /*
413 * We always use two hardware channels, because using
414 * one at 8kHz gives a nasty whining sound from the
415 * speaker. The aurateconv mechanism doesn't support
416 * ulaw, so we do the channel duplication ourselves,
417 * and don't try to do rate conversion.
418 */
419 sample_period = 1000000 / 2 / p->sample_rate;
420 if (sample_period < 3) sample_period = 3;
421 p->sample_rate = 1000000 / 2 / sample_period;
422 hw = *p;
423 hw.encoding = AUDIO_ENCODING_NONE;
424 hw.precision = 8;
425 vidcaudio_rate(sample_period - 2);
426 vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
427 if (p->channels == 1) {
428 pfil->append(pfil, mulaw_to_vidc_stereo, &hw);
429 for (ch = 0; ch < 8; ch++)
430 vidcaudio_stereo(ch, SIR_CENTRE);
431 } else {
432 pfil->append(pfil, mulaw_to_vidc, &hw);
433 for (ch = 0; ch < 8; ch += 2)
434 vidcaudio_stereo(ch, SIR_LEFT_100);
435 for (ch = 1; ch < 8; ch += 2)
436 vidcaudio_stereo(ch, SIR_RIGHT_100);
437 }
438 }
439 return 0;
440 }
441
442 static int
443 vidcaudio_round_blocksize(void *addr, int wantblk,
444 int mode, const audio_params_t *param)
445 {
446 int blk;
447
448 /*
449 * Find the smallest power of two that's larger than the
450 * requested block size, but don't allow < 32 (DMA burst is 16
451 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
452 * confined to a page).
453 */
454
455 for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
456 if (blk >= wantblk) return blk;
457 return blk;
458 }
459
460 static int
461 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
462 void (*intr)(void *), void *arg, const audio_params_t *params)
463 {
464 struct vidcaudio_softc *sc = addr;
465 size_t npages, i;
466
467 DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
468 start, end, blksize));
469
470 KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize, 0, NULL));
471 KASSERT((vaddr_t)start % blksize == 0);
472
473 sc->sc_pblksize = blksize;
474 sc->sc_pbufsize = (char *)end - (char *)start;
475 npages = sc->sc_pbufsize >> PGSHIFT;
476 if (sc->sc_ppages != NULL)
477 free(sc->sc_ppages, M_DEVBUF);
478 sc->sc_ppages = malloc(npages * sizeof(paddr_t), M_DEVBUF, M_WAITOK);
479 if (sc->sc_ppages == NULL) return ENOMEM;
480 for (i = 0; i < npages; i++)
481 if (!pmap_extract(pmap_kernel(),
482 (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
483 return EIO;
484 sc->sc_poffset = 0;
485 sc->sc_pintr = intr;
486 sc->sc_parg = arg;
487
488 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
489 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
490
491 /*
492 * Queue up the first two blocks, but don't tell audio code
493 * we're finished with them yet.
494 */
495 sc->sc_pcountdown = 2;
496
497 enable_irq(sc->sc_dma_intr);
498
499 return 0;
500 }
501
502 static int
503 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
504 void (*intr)(void *), void *arg, const audio_params_t *params)
505 {
506
507 return ENODEV;
508 }
509
510 static int
511 vidcaudio_halt_output(void *addr)
512 {
513 struct vidcaudio_softc *sc = addr;
514
515 DPRINTF(("vidcaudio_halt_output\n"));
516 disable_irq(sc->sc_dma_intr);
517 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
518 return 0;
519 }
520
521 static int
522 vidcaudio_halt_input(void *addr)
523 {
524
525 return ENODEV;
526 }
527
528 static int
529 vidcaudio_getdev(void *addr, struct audio_device *retp)
530 {
531
532 *retp = vidcaudio_device;
533 return 0;
534 }
535
536
537 static int
538 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
539 {
540
541 return EINVAL;
542 }
543
544 static int
545 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
546 {
547
548 return EINVAL;
549 }
550
551 static int
552 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
553 {
554
555 return ENXIO;
556 }
557
558 static int
559 vidcaudio_get_props(void *addr)
560 {
561
562 return 0;
563 }
564
565 static void
566 vidcaudio_rate(int rate)
567 {
568
569 WriteWord(vidc_base, VIDC_SFR | rate);
570 }
571
572 static void
573 vidcaudio_ctrl(int ctrl)
574 {
575
576 WriteWord(vidc_base, VIDC_SCR | ctrl);
577 }
578
579 static void
580 vidcaudio_stereo(int channel, int position)
581 {
582
583 channel = channel << 24 | VIDC_SIR0;
584 WriteWord(vidc_base, channel | position);
585 }
586
587 static int
588 vidcaudio_intr(void *arg)
589 {
590 struct vidcaudio_softc *sc = arg;
591 int status;
592 paddr_t pnext, pend;
593
594 status = IOMD_READ_BYTE(IOMD_SD0ST);
595 DPRINTF(("I[%x]", status));
596 if ((status & IOMD_DMAST_INT) == 0)
597 return 0;
598
599 pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
600 (sc->sc_poffset & PGOFSET);
601 pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
602
603 switch (status &
604 (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
605
606 case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
607 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
608 DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
609 IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
610 IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
611 break;
612
613 case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
614 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
615 DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
616 IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
617 IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
618 break;
619 }
620
621 sc->sc_poffset += sc->sc_pblksize;
622 if (sc->sc_poffset >= sc->sc_pbufsize)
623 sc->sc_poffset = 0;
624
625 if (sc->sc_pcountdown > 0)
626 sc->sc_pcountdown--;
627 else
628 (*sc->sc_pintr)(sc->sc_parg);
629
630 return 1;
631 }
632