vidcaudio.c revision 1.1 1 /* $NetBSD: vidcaudio.c,v 1.1 2001/10/05 22:27:43 reinoud 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 * audio driver for the RiscPC 16 bit sound
34 *
35 * Interfaces with the NetBSD generic audio driver to provide SUN
36 * /dev/audio (partial) compatibility.
37 */
38
39 #include <sys/param.h> /* proc.h */
40 #include <sys/conf.h> /* autoconfig functions */
41 #include <sys/device.h> /* device calls */
42 #include <sys/proc.h> /* device calls */
43 #include <sys/audioio.h>
44 #include <sys/errno.h>
45 #include <sys/systm.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <dev/audio_if.h>
50
51 #include <machine/irqhandler.h>
52 #include <machine/katelib.h>
53
54 #include <arm/iomd/iomdreg.h>
55 #include <arm/iomd/iomdvar.h>
56 #include <arm/iomd/vidc.h>
57 #include <arm/mainbus/mainbus.h>
58 #include <arm/iomd/waveform.h>
59 #include "vidcaudio.h"
60
61 extern int *vidc_base;
62
63 #undef DEBUG
64
65 struct audio_general {
66 vm_offset_t silence;
67 irqhandler_t ih;
68
69 void (*intr) ();
70 void *arg;
71
72 vm_offset_t next_cur;
73 vm_offset_t next_end;
74 void (*next_intr) ();
75 void *next_arg;
76
77 int buffer;
78 int in_progress;
79
80 int open;
81 } ag;
82
83 struct vidcaudio_softc {
84 struct device device;
85 int iobase;
86
87 int open;
88 };
89
90 int vidcaudio_probe __P((struct device *parent, struct cfdata *cf, void *aux));
91 void vidcaudio_attach __P((struct device *parent, struct device *self, void *aux));
92 int vidcaudio_open __P((void *addr, int flags));
93 void vidcaudio_close __P((void *addr));
94
95 int vidcaudio_intr __P((void *arg));
96 int vidcaudio_dma_program __P((vm_offset_t cur, vm_offset_t end, void (*intr)(), void *arg));
97 void vidcaudio_dummy_routine __P((void *arg));
98 int vidcaudio_stereo __P((int channel, int position));
99 int vidcaudio_rate __P((int rate));
100 void vidcaudio_shutdown __P((void));
101
102 static int sound_dma_intr;
103
104 struct cfattach vidcaudio_ca = {
105 sizeof(struct vidcaudio_softc), vidcaudio_probe, vidcaudio_attach
106 };
107
108 int vidcaudio_query_encoding __P((void *, struct audio_encoding *));
109 int vidcaudio_set_params __P((void *, int, int, struct audio_params *, struct audio_params *));
110 int vidcaudio_round_blocksize __P((void *, int));
111 int vidcaudio_start_output __P((void *, void *, int, void (*)(), void *));
112 int vidcaudio_start_input __P((void *, void *, int, void (*)(), void *));
113 int vidcaudio_halt_output __P((void *));
114 int vidcaudio_halt_input __P((void *));
115 int vidcaudio_speaker_ctl __P((void *, int));
116 int vidcaudio_getdev __P((void *, struct audio_device *));
117 int vidcaudio_set_port __P((void *, mixer_ctrl_t *));
118 int vidcaudio_get_port __P((void *, mixer_ctrl_t *));
119 int vidcaudio_query_devinfo __P((void *, mixer_devinfo_t *));
120 int vidcaudio_get_props __P((void *));
121
122 struct audio_device vidcaudio_device = {
123 "VidcAudio 8-bit",
124 "x",
125 "vidcaudio"
126 };
127
128 struct audio_hw_if vidcaudio_hw_if = {
129 vidcaudio_open,
130 vidcaudio_close,
131 0,
132 vidcaudio_query_encoding,
133 vidcaudio_set_params,
134 vidcaudio_round_blocksize,
135 0,
136 0,
137 0,
138 vidcaudio_start_output,
139 vidcaudio_start_input,
140 vidcaudio_halt_output,
141 vidcaudio_halt_input,
142 vidcaudio_speaker_ctl,
143 vidcaudio_getdev,
144 0,
145 vidcaudio_set_port,
146 vidcaudio_get_port,
147 vidcaudio_query_devinfo,
148 0,
149 0,
150 0,
151 0,
152 vidcaudio_get_props,
153 0,
154 0,
155 0,
156 };
157
158
159 void
160 vidcaudio_beep_generate()
161 {
162 vidcaudio_dma_program(ag.silence, ag.silence+sizeof(beep_waveform)-16,
163 vidcaudio_dummy_routine, NULL);
164 }
165
166
167 int
168 vidcaudio_probe(parent, cf, aux)
169 struct device *parent;
170 struct cfdata* cf;
171 void *aux;
172 {
173 int id;
174
175 id = IOMD_ID;
176
177 /* So far I only know about this IOMD */
178 switch (id) {
179 case RPC600_IOMD_ID:
180 return(1);
181 break;
182 case ARM7500_IOC_ID:
183 case ARM7500FE_IOC_ID:
184 #ifdef RC7500
185 return(0);
186 #else
187 return(1);
188 #endif
189 break;
190 default:
191 printf("vidcaudio: Unknown IOMD id=%04x", id);
192 break;
193 }
194
195 return (0);
196 }
197
198
199 void
200 vidcaudio_attach(parent, self, aux)
201 struct device *parent;
202 struct device *self;
203 void *aux;
204 {
205 struct mainbus_attach_args *mb = aux;
206 struct vidcaudio_softc *sc = (void *)self;
207 int id;
208
209 sc->iobase = mb->mb_iobase;
210
211 sc->open = 0;
212 ag.in_progress = 0;
213
214 ag.next_cur = 0;
215 ag.next_end = 0;
216 ag.next_intr = NULL;
217 ag.next_arg = NULL;
218
219 vidcaudio_rate(32); /* 24*1024*/
220
221 /* Program the silence buffer and reset the DMA channel */
222 ag.silence = uvm_km_alloc(kernel_map, NBPG);
223 if (ag.silence == NULL)
224 panic("vidcaudio: Cannot allocate memory\n");
225
226 memset((char *)ag.silence, 0, NBPG);
227 memcpy((char *)ag.silence, (char *)beep_waveform, sizeof(beep_waveform));
228
229 ag.buffer = 0;
230
231 /* Install the irq handler for the DMA interrupt */
232 ag.ih.ih_func = vidcaudio_intr;
233 ag.ih.ih_arg = NULL;
234 ag.ih.ih_level = IPL_AUDIO;
235 ag.ih.ih_name = "vidcaudio";
236
237 ag.intr = NULL;
238 /* ag.nextintr = NULL;*/
239
240 id = IOMD_ID;
241
242 switch (id) {
243 case RPC600_IOMD_ID:
244 sound_dma_intr = IRQ_DMASCH0;
245 break;
246 case ARM7500_IOC_ID:
247 case ARM7500FE_IOC_ID:
248 sound_dma_intr = IRQ_SDMA;
249 break;
250 }
251
252 disable_irq(sound_dma_intr);
253
254 if (irq_claim(sound_dma_intr, &(ag.ih)))
255 panic("vidcaudio: couldn't claim IRQ %d\n", sound_dma_intr);
256
257 disable_irq(sound_dma_intr);
258
259 printf("\n");
260
261 vidcaudio_dma_program(ag.silence, ag.silence+NBPG-16,
262 vidcaudio_dummy_routine, NULL);
263
264 audio_attach_mi(&vidcaudio_hw_if, sc, &sc->device);
265
266 #ifdef DEBUG
267 printf(" UNDER DEVELOPMENT (nuts)\n");
268 #endif
269 }
270
271 int
272 vidcaudio_open(addr, flags)
273 void *addr;
274 int flags;
275 {
276 struct vidcaudio_softc *sc = addr;
277
278 #ifdef DEBUG
279 printf("DEBUG: vidcaudio_open called\n");
280 #endif
281
282 if (sc->open)
283 return EBUSY;
284
285 sc->open = 1;
286 ag.open = 1;
287
288 return 0;
289 }
290
291 void
292 vidcaudio_close(addr)
293 void *addr;
294 {
295 struct vidcaudio_softc *sc = addr;
296
297 vidcaudio_shutdown();
298
299 #ifdef DEBUG
300 printf("DEBUG: vidcaudio_close called\n");
301 #endif
302
303 sc->open = 0;
304 ag.open = 0;
305 }
306
307 /* ************************************************************************* *
308 | Interface to the generic audio driver |
309 * ************************************************************************* */
310
311 int
312 vidcaudio_query_encoding(addr, fp)
313 void *addr;
314 struct audio_encoding *fp;
315 {
316 switch (fp->index) {
317 case 0:
318 strcpy(fp->name, "vidc");
319 fp->encoding = AUDIO_ENCODING_ULAW;
320 fp->precision = 8;
321 fp->flags = 0;
322 break;
323
324 default:
325 return(EINVAL);
326 }
327 return 0;
328 }
329
330 int
331 vidcaudio_set_params(addr, setmode, usemode, p, r)
332 void *addr;
333 int setmode, usemode;
334 struct audio_params *p, *r;
335 {
336 if (p->encoding != AUDIO_ENCODING_ULAW ||
337 p->channels != 8)
338 return EINVAL;
339 vidcaudio_rate(4 * p->sample_rate / (3 * 1024)); /* XXX probably wrong */
340
341 return 0;
342 }
343
344 int
345 vidcaudio_round_blocksize(addr, blk)
346 void *addr;
347 int blk;
348 {
349 if (blk > NBPG)
350 blk = NBPG;
351 return (blk);
352 }
353
354 #define ROUND(s) ( ((int)s) & (~(NBPG-1)) )
355
356 int
357 vidcaudio_start_output(addr, p, cc, intr, arg)
358 void *addr;
359 void *p;
360 int cc;
361 void (*intr)();
362 void *arg;
363 {
364 /* I can only DMA inside 1 page */
365
366 #ifdef DEBUG
367 printf("vidcaudio_start_output (%d) %08x %08x\n", cc, intr, arg);
368 #endif
369
370 if (ROUND(p) != ROUND(p+cc)) {
371 /*
372 * If it's over a page I can fix it up by copying it into
373 * my buffer
374 */
375
376 #ifdef DEBUG
377 printf("vidcaudio: DMA over page boundary requested."
378 " Fixing up\n");
379 #endif
380 memcpy(p, (char *)ag.silence, cc > NBPG ? NBPG : cc);
381 p = (void *)ag.silence;
382
383 /*
384 * I can't DMA any more than that, but it is possible to
385 * fix it up by handling multiple buffers and only
386 * interrupting the audio driver after sending out all the
387 * stuff it gave me. That it more than I can be bothered
388 * to do right now and it probablly wont happen so I'll just
389 * truncate the buffer and tell the user.
390 */
391
392 if (cc > NBPG) {
393 printf("vidcaudio: DMA buffer truncated. I could fix this up\n");
394 cc = NBPG;
395 }
396 }
397 vidcaudio_dma_program((vm_offset_t)p, (vm_offset_t)((char *)p+cc),
398 intr, arg);
399 return 0;
400 }
401
402 int
403 vidcaudio_start_input(addr, p, cc, intr, arg)
404 void *addr;
405 void *p;
406 int cc;
407 void (*intr)();
408 void *arg;
409 {
410 return EIO;
411 }
412
413 int
414 vidcaudio_halt_output(addr)
415 void *addr;
416 {
417 #ifdef DEBUG
418 printf("DEBUG: vidcaudio_halt_output\n");
419 #endif
420 return EIO;
421 }
422
423 int
424 vidcaudio_halt_input(addr)
425 void *addr;
426 {
427 #ifdef DEBUG
428 printf("DEBUG: vidcaudio_halt_input\n");
429 #endif
430 return EIO;
431 }
432
433 int
434 vidcaudio_speaker_ctl(addr, newstate)
435 void *addr;
436 int newstate;
437 {
438 #ifdef DEBUG
439 printf("DEBUG: vidcaudio_speaker_ctl\n");
440 #endif
441 return 0;
442 }
443
444 int
445 vidcaudio_getdev(addr, retp)
446 void *addr;
447 struct audio_device *retp;
448 {
449 *retp = vidcaudio_device;
450 return 0;
451 }
452
453
454 int
455 vidcaudio_set_port(addr, cp)
456 void *addr;
457 mixer_ctrl_t *cp;
458 {
459 return EINVAL;
460 }
461
462 int
463 vidcaudio_get_port(addr, cp)
464 void *addr;
465 mixer_ctrl_t *cp;
466 {
467 return EINVAL;
468 }
469
470 int
471 vidcaudio_query_devinfo(addr, dip)
472 void *addr;
473 mixer_devinfo_t *dip;
474 {
475 return ENXIO;
476 }
477
478 int
479 vidcaudio_get_props(addr)
480 void *addr;
481 {
482 return 0;
483 }
484 void
485 vidcaudio_dummy_routine(arg)
486 void *arg;
487 {
488 #ifdef DEBUG
489 printf("vidcaudio_dummy_routine\n");
490 #endif
491 }
492
493 int
494 vidcaudio_rate(rate)
495 int rate;
496 {
497 WriteWord(vidc_base, VIDC_SFR | rate);
498 return 0;
499 }
500
501 int
502 vidcaudio_stereo(channel, position)
503 int channel;
504 int position;
505 {
506 if (channel < 0) return EINVAL;
507 if (channel > 7) return EINVAL;
508 channel = channel<<24 | VIDC_SIR0;
509 WriteWord(vidc_base, channel | position);
510 return 0;
511 }
512
513 #define PHYS(x, y) pmap_extract(pmap_kernel(), ((x)&PG_FRAME), (paddr_t *)(y))
514
515 /*
516 * Program the next buffer to be used
517 * This function must be re-entrant, maximum re-entrancy of 2
518 */
519
520 #define FLAGS (0)
521
522 int
523 vidcaudio_dma_program(cur, end, intr, arg)
524 vm_offset_t cur;
525 vm_offset_t end;
526 void (*intr)();
527 void *arg;
528 {
529 paddr_t pa1, pa2;
530
531 /* If there isn't a transfer in progress then start a new one */
532 if (ag.in_progress == 0) {
533 ag.buffer = 0;
534 IOMD_WRITE_WORD(IOMD_SD0CR, 0x90); /* Reset State Machine */
535 IOMD_WRITE_WORD(IOMD_SD0CR, 0x30); /* Reset State Machine */
536
537 PHYS(cur, &pa1);
538 PHYS(end - 16, &pa2);
539
540 IOMD_WRITE_WORD(IOMD_SD0CURB, pa1);
541 IOMD_WRITE_WORD(IOMD_SD0ENDB, pa2|FLAGS);
542 IOMD_WRITE_WORD(IOMD_SD0CURA, pa1);
543 IOMD_WRITE_WORD(IOMD_SD0ENDA, pa2|FLAGS);
544
545 ag.in_progress = 1;
546
547 ag.next_cur = ag.next_end = 0;
548 ag.next_intr = ag.next_arg = 0;
549
550 ag.intr = intr;
551 ag.arg = arg;
552
553 /*
554 * The driver 'clicks' between buffer swaps, leading me
555 * to think that the fifo is much small than on other
556 * sound cards so I'm going to have to do some tricks here
557 */
558
559 (*ag.intr)(ag.arg); /* Schedule the next buffer */
560 ag.intr = vidcaudio_dummy_routine; /* Already done this */
561 ag.arg = NULL;
562
563 #ifdef PRINT
564 printf("vidcaudio: start output\n");
565 #endif
566 #ifdef DEBUG
567 printf("SE");
568 #endif
569 enable_irq(sound_dma_intr);
570 } else {
571 /* Otherwise schedule the next one */
572 if (ag.next_cur != 0) {
573 /* If there's one scheduled then complain */
574 printf("vidcaudio: Buffer already Q'ed\n");
575 return EIO;
576 } else {
577 /* We're OK to schedule it now */
578 ag.buffer = (++ag.buffer) & 1;
579 PHYS(cur, &ag.next_cur);
580 PHYS(end - 16, &ag.next_end);
581 ag.next_intr = intr;
582 ag.next_arg = arg;
583 #ifdef DEBUG
584 printf("s");
585 #endif
586 }
587 }
588 return 0;
589 }
590
591 void
592 vidcaudio_shutdown(void)
593 {
594 /* Shut down the channel */
595 ag.intr = NULL;
596 ag.in_progress = 0;
597 #ifdef PRINT
598 printf("vidcaudio: stop output\n");
599 #endif
600 IOMD_WRITE_WORD(IOMD_SD0CURB, ag.silence);
601 IOMD_WRITE_WORD(IOMD_SD0ENDB, (ag.silence + NBPG - 16) | (1<<30));
602 disable_irq(sound_dma_intr);
603 }
604
605 int
606 vidcaudio_intr(arg)
607 void *arg;
608 {
609 int status = IOMD_READ_BYTE(IOMD_SD0ST);
610 void (*nintr)();
611 void *narg;
612 void (*xintr)();
613 void *xarg;
614 int xcur, xend;
615 IOMD_WRITE_WORD(IOMD_DMARQ, 0x10);
616
617 #ifdef PRINT
618 printf ( "I" );
619 #endif
620
621 if (ag.open == 0) {
622 vidcaudio_shutdown();
623 return 0;
624 }
625
626 /* Have I got the generic audio device attached */
627
628 #ifdef DEBUG
629 printf ( "[B%01x]", status );
630 #endif
631
632 nintr = ag.intr;
633 narg = ag.arg;
634 ag.intr = NULL;
635
636 xintr = ag.next_intr;
637 xarg = ag.next_arg;
638 xcur = ag.next_cur;
639 xend = ag.next_end;
640 ag.next_cur = 0;
641 ag.intr = xintr;
642 ag.arg = xarg;
643
644 if (nintr) {
645 #ifdef DEBUG
646 printf("i");
647 #endif
648 (*nintr)(narg);
649 }
650
651 if (xcur == 0) {
652 vidcaudio_shutdown ();
653 } else {
654 #define OVERRUN (0x04)
655 #define INTERRUPT (0x02)
656 #define BANK_A (0x00)
657 #define BANK_B (0x01)
658 switch (status & 0x7) {
659 case (INTERRUPT|BANK_A):
660 #ifdef PRINT
661 printf("B");
662 #endif
663 IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
664 IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
665 break;
666
667 case (INTERRUPT|BANK_B):
668 #ifdef PRINT
669 printf("A");
670 #endif
671 IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
672 IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
673 break;
674
675 case (OVERRUN|INTERRUPT|BANK_A):
676 #ifdef PRINT
677 printf("A");
678 #endif
679 IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
680 IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
681 break;
682
683 case (OVERRUN|INTERRUPT|BANK_B):
684 #ifdef PRINT
685 printf("B");
686 #endif
687 IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
688 IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
689 break;
690 }
691 /*
692 ag.next_cur = 0;
693 ag.intr = xintr;
694 ag.arg = xarg;
695 */
696 }
697 #ifdef PRINT
698 printf ( "i" );
699 #endif
700
701 if (ag.next_cur == 0) {
702 (*ag.intr)(ag.arg); /* Schedule the next buffer */
703 ag.intr = vidcaudio_dummy_routine; /* Already done this */
704 ag.arg = NULL;
705 }
706 return(0); /* Pass interrupt on down the chain */
707 }
708