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