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