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