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