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