vidcaudio.c revision 1.37 1 /* $NetBSD: vidcaudio.c,v 1.37 2004/01/18 14:42:09 bjh21 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.37 2004/01/18 14:42:09 bjh21 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/mulaw.h>
83
84 #include <machine/intr.h>
85 #include <machine/machdep.h>
86 #include <arm/arm32/katelib.h>
87
88 #include <arm/iomd/vidcaudiovar.h>
89 #include <arm/iomd/iomdreg.h>
90 #include <arm/iomd/iomdvar.h>
91 #include <arm/iomd/rpckbdvar.h>
92 #include <arm/iomd/vidc.h>
93 #include <arm/mainbus/mainbus.h>
94 #include <arm/iomd/waveform.h>
95
96 #include "rpckbd.h"
97 #include "vt.h"
98
99 extern void vt_hookup_bell(void (*)(void *, u_int, u_int, u_int, int), void *);
100
101 extern int *vidc_base;
102
103 #ifdef VIDCAUDIO_DEBUG
104 #define DPRINTF(x) printf x
105 #else
106 #define DPRINTF(x)
107 #endif
108
109 struct vidcaudio_softc {
110 struct device sc_dev;
111
112 irqhandler_t sc_ih;
113 int sc_dma_intr;
114
115 int sc_is16bit;
116
117 size_t sc_pblksize;
118 vm_offset_t sc_poffset;
119 vm_offset_t sc_pbufsize;
120 paddr_t *sc_ppages;
121 void (*sc_pintr)(void *);
122 void *sc_parg;
123 int sc_pcountdown;
124 };
125
126 static int vidcaudio_probe(struct device *, struct cfdata *, void *);
127 static void vidcaudio_attach(struct device *, struct device *, void *);
128 static int vidcaudio_open(void *, int);
129 static void vidcaudio_close(void *);
130
131 static int vidcaudio_intr(void *);
132 static void vidcaudio_rate(int);
133 static void vidcaudio_ctrl(int);
134 static void vidcaudio_stereo(int, int);
135 static void mulaw_to_vidc(void *, u_char *, int);
136 static void mulaw_to_vidc_stereo(void *, u_char *, int);
137
138 CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
139 vidcaudio_probe, vidcaudio_attach, NULL, NULL);
140
141 static int vidcaudio_query_encoding(void *, struct audio_encoding *);
142 static int vidcaudio_set_params(void *, int, int, struct audio_params *,
143 struct audio_params *);
144 static int vidcaudio_round_blocksize(void *, int);
145 static int vidcaudio_trigger_output(void *, void *, void *, int,
146 void (*)(void *), void *, struct audio_params *);
147 static int vidcaudio_trigger_input(void *, void *, void *, int,
148 void (*)(void *), void *, struct audio_params *);
149 static int vidcaudio_halt_output(void *);
150 static int vidcaudio_halt_input(void *);
151 static int vidcaudio_getdev(void *, struct audio_device *);
152 static int vidcaudio_set_port(void *, mixer_ctrl_t *);
153 static int vidcaudio_get_port(void *, mixer_ctrl_t *);
154 static int vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
155 static int vidcaudio_get_props(void *);
156
157 static struct audio_device vidcaudio_device = {
158 "ARM VIDC",
159 "",
160 "vidcaudio"
161 };
162
163 static struct audio_hw_if vidcaudio_hw_if = {
164 vidcaudio_open,
165 vidcaudio_close,
166 NULL,
167 vidcaudio_query_encoding,
168 vidcaudio_set_params,
169 vidcaudio_round_blocksize,
170 NULL,
171 NULL,
172 NULL,
173 NULL,
174 NULL,
175 vidcaudio_halt_output,
176 vidcaudio_halt_input,
177 NULL,
178 vidcaudio_getdev,
179 NULL,
180 vidcaudio_set_port,
181 vidcaudio_get_port,
182 vidcaudio_query_devinfo,
183 NULL,
184 NULL,
185 NULL,
186 NULL,
187 vidcaudio_get_props,
188 vidcaudio_trigger_output,
189 vidcaudio_trigger_input,
190 NULL,
191 };
192
193 static int
194 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
195 {
196 int id;
197
198 id = IOMD_ID;
199
200 /* So far I only know about this IOMD */
201 switch (id) {
202 case RPC600_IOMD_ID:
203 case ARM7500_IOC_ID:
204 case ARM7500FE_IOC_ID:
205 return 1;
206 default:
207 aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
208 return 0;
209 }
210 }
211
212
213 static void
214 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
215 {
216 struct vidcaudio_softc *sc = (void *)self;
217 struct device *beepdev;
218
219 switch (IOMD_ID) {
220 #ifndef EB7500ATX
221 case RPC600_IOMD_ID:
222 sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
223 sc->sc_dma_intr = IRQ_DMASCH0;
224 break;
225 #endif
226 case ARM7500_IOC_ID:
227 case ARM7500FE_IOC_ID:
228 sc->sc_is16bit = TRUE;
229 sc->sc_dma_intr = IRQ_SDMA;
230 break;
231 default:
232 aprint_error(": strange IOMD\n");
233 return;
234 }
235
236 if (sc->sc_is16bit)
237 aprint_normal(": 16-bit external DAC\n");
238 else
239 aprint_normal(": 8-bit internal DAC\n");
240
241 /* Install the irq handler for the DMA interrupt */
242 sc->sc_ih.ih_func = vidcaudio_intr;
243 sc->sc_ih.ih_arg = sc;
244 sc->sc_ih.ih_level = IPL_AUDIO;
245 sc->sc_ih.ih_name = self->dv_xname;
246
247 if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
248 aprint_error("%s: couldn't claim IRQ %d\n",
249 self->dv_xname, sc->sc_dma_intr);
250 return;
251 }
252
253 disable_irq(sc->sc_dma_intr);
254
255 beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
256 #if NRPCKBD > 0
257 rpckbd_hookup_bell(audiobell, beepdev);
258 #endif
259 #if NVT > 0
260 vt_hookup_bell(audiobell, beepdev);
261 #endif
262 }
263
264 static int
265 vidcaudio_open(void *addr, int flags)
266 {
267
268 DPRINTF(("DEBUG: vidcaudio_open called\n"));
269 return 0;
270 }
271
272 static void
273 vidcaudio_close(void *addr)
274 {
275 struct vidcaudio_softc *sc = addr;
276
277 DPRINTF(("DEBUG: vidcaudio_close called\n"));
278 /*
279 * We do this here rather than in vidcaudio_halt_output()
280 * because the latter can be called from interrupt context
281 * (audio_pint()->audio_clear()->vidcaudio_halt_output()).
282 */
283 if (sc->sc_ppages != NULL) {
284 free(sc->sc_ppages, M_DEVBUF);
285 sc->sc_ppages = NULL;
286 }
287 }
288
289 /*
290 * Interface to the generic audio driver
291 */
292
293 static int
294 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
295 {
296 struct vidcaudio_softc *sc = addr;
297
298 switch (fp->index) {
299 case 0:
300 strcpy(fp->name, AudioEmulaw);
301 fp->encoding = AUDIO_ENCODING_ULAW;
302 fp->precision = 8;
303 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
304 break;
305
306 case 1:
307 if (sc->sc_is16bit) {
308 strcpy(fp->name, AudioEslinear_le);
309 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
310 fp->precision = 16;
311 fp->flags = 0;
312 break;
313 }
314 /* FALLTHROUGH */
315 default:
316 return EINVAL;
317 }
318 return 0;
319 }
320
321 #define MULAW_TO_VIDC(m) (~((m) << 1 | (m) >> 7))
322
323 static void
324 mulaw_to_vidc(void *v, u_char *p, int cc)
325 {
326
327 while (--cc >= 0) {
328 *p = MULAW_TO_VIDC(*p);
329 ++p;
330 }
331 }
332
333 static void
334 mulaw_to_vidc_stereo(void *v, u_char *p, int cc)
335 {
336 u_char *q = p;
337
338 p += cc;
339 q += cc * 2;
340 while (--cc >= 0) {
341 q -= 2;
342 --p;
343 q[0] = q[1] = MULAW_TO_VIDC(*p);
344 }
345 }
346
347 static int
348 vidcaudio_set_params(void *addr, int setmode, int usemode,
349 struct audio_params *p, struct audio_params *r)
350 {
351 struct vidcaudio_softc *sc = addr;
352 int sample_period, ch;
353
354 if ((setmode & AUMODE_PLAY) == 0)
355 return 0;
356
357 if (sc->sc_is16bit) {
358 /* ARM7500ish, 16-bit, two-channel */
359 if (p->encoding == AUDIO_ENCODING_ULAW && p->precision == 8) {
360 p->sw_code = mulaw_to_slinear16_le;
361 p->factor = 2; p->factor_denom = 1;
362 } else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
363 p->precision != 16)
364 return EINVAL;
365 p->hw_channels = 2;
366 sample_period = 705600 / 4 / p->sample_rate;
367 if (sample_period < 3) sample_period = 3;
368 p->hw_sample_rate = 705600 / 4 / sample_period;
369 vidcaudio_rate(sample_period - 2);
370 vidcaudio_ctrl(SCR_SERIAL);
371 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
372 p->hw_precision = 16;
373 } else {
374 /* VIDC20ish, u-law, 8-channel */
375 if (p->encoding != AUDIO_ENCODING_ULAW || p->precision != 8)
376 return EINVAL;
377 /*
378 * We always use two hardware channels, because using
379 * one at 8kHz gives a nasty whining sound from the
380 * speaker. The aurateconv mechanism doesn't support
381 * ulaw, so we do the channel duplication ourselves,
382 * and don't try to do rate conversion.
383 */
384 switch (p->channels) {
385 case 1:
386 p->sw_code = mulaw_to_vidc_stereo;
387 p->factor = 2; p->factor_denom = 1;
388 break;
389 case 2:
390 p->sw_code = mulaw_to_vidc;
391 p->factor = 1; p->factor_denom = 1;
392 break;
393 default:
394 return EINVAL;
395 }
396 p->hw_channels = p->channels;
397 sample_period = 1000000 / 2 / p->sample_rate;
398 if (sample_period < 3) sample_period = 3;
399 p->hw_sample_rate = p->sample_rate =
400 1000000 / 2 / sample_period;
401 p->hw_encoding = AUDIO_ENCODING_NONE;
402 p->hw_precision = 8;
403 vidcaudio_rate(sample_period - 2);
404 vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
405 if (p->hw_channels == 1)
406 for (ch = 0; ch < 8; ch++)
407 vidcaudio_stereo(ch, SIR_CENTRE);
408 else {
409 for (ch = 0; ch < 8; ch += 2)
410 vidcaudio_stereo(ch, SIR_LEFT_100);
411 for (ch = 1; ch < 8; ch += 2)
412 vidcaudio_stereo(ch, SIR_RIGHT_100);
413 }
414 }
415 return 0;
416 }
417
418 static int
419 vidcaudio_round_blocksize(void *addr, int wantblk)
420 {
421 int blk;
422
423 /*
424 * Find the smallest power of two that's larger than the
425 * requested block size, but don't allow < 32 (DMA burst is 16
426 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
427 * confined to a page).
428 */
429
430 for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
431 if (blk >= wantblk) return blk;
432 return blk;
433 }
434
435 static int
436 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
437 void (*intr)(void *), void *arg, struct audio_params *params)
438 {
439 struct vidcaudio_softc *sc = addr;
440 size_t npages, i;
441
442 DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
443 start, end, blksize));
444
445 KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize));
446 KASSERT((vaddr_t)start % blksize == 0);
447
448 sc->sc_pblksize = blksize;
449 sc->sc_pbufsize = (char *)end - (char *)start;
450 npages = sc->sc_pbufsize >> PGSHIFT;
451 if (sc->sc_ppages != NULL)
452 free(sc->sc_ppages, M_DEVBUF);
453 sc->sc_ppages = malloc(npages * sizeof(paddr_t), M_DEVBUF, M_WAITOK);
454 if (sc->sc_ppages == NULL) return ENOMEM;
455 for (i = 0; i < npages; i++)
456 if (!pmap_extract(pmap_kernel(),
457 (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
458 return EIO;
459 sc->sc_poffset = 0;
460 sc->sc_pintr = intr;
461 sc->sc_parg = arg;
462
463 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
464 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
465
466 /*
467 * Queue up the first two blocks, but don't tell audio code
468 * we're finished with them yet.
469 */
470 sc->sc_pcountdown = 2;
471
472 enable_irq(sc->sc_dma_intr);
473
474 return 0;
475 }
476
477 static int
478 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
479 void (*intr)(void *), void *arg, struct audio_params *params)
480 {
481
482 return ENODEV;
483 }
484
485 static int
486 vidcaudio_halt_output(void *addr)
487 {
488 struct vidcaudio_softc *sc = addr;
489
490 DPRINTF(("vidcaudio_halt_output\n"));
491 disable_irq(sc->sc_dma_intr);
492 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
493 return 0;
494 }
495
496 static int
497 vidcaudio_halt_input(void *addr)
498 {
499
500 return ENODEV;
501 }
502
503 static int
504 vidcaudio_getdev(void *addr, struct audio_device *retp)
505 {
506
507 *retp = vidcaudio_device;
508 return 0;
509 }
510
511
512 static int
513 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
514 {
515
516 return EINVAL;
517 }
518
519 static int
520 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
521 {
522
523 return EINVAL;
524 }
525
526 static int
527 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
528 {
529
530 return ENXIO;
531 }
532
533 static int
534 vidcaudio_get_props(void *addr)
535 {
536
537 return 0;
538 }
539
540 static void
541 vidcaudio_rate(int rate)
542 {
543
544 WriteWord(vidc_base, VIDC_SFR | rate);
545 }
546
547 static void
548 vidcaudio_ctrl(int ctrl)
549 {
550
551 WriteWord(vidc_base, VIDC_SCR | ctrl);
552 }
553
554 static void
555 vidcaudio_stereo(int channel, int position)
556 {
557
558 channel = channel << 24 | VIDC_SIR0;
559 WriteWord(vidc_base, channel | position);
560 }
561
562 static int
563 vidcaudio_intr(void *arg)
564 {
565 struct vidcaudio_softc *sc = arg;
566 int status;
567 paddr_t pnext, pend;
568
569 status = IOMD_READ_BYTE(IOMD_SD0ST);
570 DPRINTF(("I[%x]", status));
571 if ((status & IOMD_DMAST_INT) == 0)
572 return 0;
573
574 pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
575 (sc->sc_poffset & PGOFSET);
576 pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
577
578 switch (status &
579 (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
580
581 case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
582 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
583 DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
584 IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
585 IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
586 break;
587
588 case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
589 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
590 DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
591 IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
592 IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
593 break;
594 }
595
596 sc->sc_poffset += sc->sc_pblksize;
597 if (sc->sc_poffset >= sc->sc_pbufsize)
598 sc->sc_poffset = 0;
599
600 if (sc->sc_pcountdown > 0)
601 sc->sc_pcountdown--;
602 else
603 (*sc->sc_pintr)(sc->sc_parg);
604
605 return 1;
606 }
607