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