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