vidcaudio.c revision 1.28 1 /* $NetBSD: vidcaudio.c,v 1.28 2004/01/01 16:35:35 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.28 2004/01/01 16:35:35 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/proc.h> /* device calls */
75 #include <sys/systm.h>
76
77 #include <uvm/uvm_extern.h>
78
79 #include <dev/audio_if.h>
80 #include <dev/mulaw.h>
81
82 #include <machine/intr.h>
83 #include <machine/machdep.h>
84 #include <arm/arm32/katelib.h>
85
86 #include <arm/iomd/vidcaudiovar.h>
87 #include <arm/iomd/iomdreg.h>
88 #include <arm/iomd/iomdvar.h>
89 #include <arm/iomd/vidc.h>
90 #include <arm/mainbus/mainbus.h>
91 #include <arm/iomd/waveform.h>
92 #include "vidcaudio.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 void *sc_pstart;
111 void *sc_pend;
112 size_t sc_pblksize;
113 void *sc_pnext;
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
268 DPRINTF(("DEBUG: vidcaudio_close called\n"));
269 }
270
271 /*
272 * Interface to the generic audio driver
273 */
274
275 static int
276 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
277 {
278 struct vidcaudio_softc *sc = addr;
279
280 switch (fp->index) {
281 case 0:
282 strcpy(fp->name, AudioEmulaw);
283 fp->encoding = AUDIO_ENCODING_ULAW;
284 fp->precision = 8;
285 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
286 break;
287
288 case 1:
289 if (sc->sc_is16bit) {
290 strcpy(fp->name, AudioEslinear_le);
291 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
292 fp->precision = 16;
293 fp->flags = 0;
294 break;
295 }
296 /* FALLTHROUGH */
297 default:
298 return EINVAL;
299 }
300 return 0;
301 }
302
303 #define MULAW_TO_VIDC(m) (~(m) << 1 | (m) >> 7)
304
305 static void
306 mulaw_to_vidc(void *v, u_char *p, int cc)
307 {
308
309 while (--cc >= 0) {
310 *p = MULAW_TO_VIDC(*p);
311 ++p;
312 }
313 }
314
315 static void
316 mulaw_to_vidc_stereo(void *v, u_char *p, int cc)
317 {
318 u_char *q = p;
319
320 p += cc;
321 q += cc * 2;
322 while (--cc >= 0) {
323 q -= 2;
324 --p;
325 q[0] = q[1] = MULAW_TO_VIDC(*p);
326 }
327 }
328
329 static int
330 vidcaudio_set_params(void *addr, int setmode, int usemode,
331 struct audio_params *p, struct audio_params *r)
332 {
333 struct vidcaudio_softc *sc = addr;
334 int sample_period, ch;
335
336 if (setmode & AUMODE_PLAY) {
337 if (sc->sc_is16bit) {
338 /* ARM7500ish, 16-bit, two-channel */
339 if (p->encoding == AUDIO_ENCODING_ULAW &&
340 p->precision == 8) {
341 p->sw_code = mulaw_to_slinear16_le;
342 p->factor = 2; p->factor_denom = 1;
343 } else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
344 p->precision != 16)
345 return EINVAL;
346 p->hw_channels = 2;
347 sample_period = 705600 / 4 / p->sample_rate;
348 if (sample_period < 3) sample_period = 3;
349 p->hw_sample_rate =
350 705600 / 4 / sample_period;
351 vidcaudio_rate(sample_period - 2);
352 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
353 p->hw_precision = 16;
354 } else {
355 /* VIDC20ish, u-law, 8-channel */
356 if (p->encoding != AUDIO_ENCODING_ULAW ||
357 p->precision != 8)
358 return EINVAL;
359 /*
360 * We always use two hardware channels,
361 * because using one at 8kHz gives a nasty
362 * whining sound from the speaker. The
363 * aurateconv mechanism doesn't support ulaw,
364 * so we do the channel duplication ourselves,
365 * and don't try to do rate conversion.
366 */
367 switch (p->channels) {
368 case 1:
369 p->sw_code = mulaw_to_vidc_stereo;
370 p->factor = 2; p->factor_denom = 1;
371 break;
372 case 2:
373 p->sw_code = mulaw_to_vidc;
374 p->factor = 1; p->factor_denom = 1;
375 break;
376 default:
377 return EINVAL;
378 }
379 p->hw_channels = p->channels;
380 sample_period =
381 1000000 / 2 / p->sample_rate;
382 if (sample_period < 3) sample_period = 3;
383 p->hw_sample_rate = p->sample_rate =
384 1000000 / 2 / sample_period;
385 p->hw_encoding = AUDIO_ENCODING_NONE;
386 p->hw_precision = 8;
387 vidcaudio_rate(sample_period - 2);
388 vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
389 if (p->hw_channels == 1)
390 for (ch = 0; ch < 8; ch++)
391 vidcaudio_stereo(ch, SIR_CENTRE);
392 else
393 for (ch = 0; ch < 8; ch++)
394 vidcaudio_stereo(ch, ch & 1 ?
395 SIR_LEFT_100 : SIR_RIGHT_100);
396 }
397 }
398 return 0;
399 }
400
401 static int
402 vidcaudio_round_blocksize(void *addr, int wantblk)
403 {
404 int blk;
405
406 /*
407 * Find the smallest power of two that's larger than the
408 * requested block size, but don't allow < 32 (DMA burst is 16
409 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
410 * confined to a page).
411 */
412
413 for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
414 if (blk >= wantblk) return blk;
415 return blk;
416 }
417
418 static int
419 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
420 void (*intr)(void *), void *arg, struct audio_params *params)
421 {
422 struct vidcaudio_softc *sc = addr;
423
424 KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize));
425
426 DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
427 start, end, blksize));
428
429 sc->sc_pstart = start;
430 sc->sc_pend = end;
431 sc->sc_pblksize = blksize;
432 sc->sc_pnext = start;
433 sc->sc_pintr = intr;
434 sc->sc_parg = arg;
435
436 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
437 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
438
439 /*
440 * Queue up the first two blocks, but don't tell audio code
441 * we're finished with them yet.
442 */
443 sc->sc_pcountdown = 2;
444
445 enable_irq(sc->sc_dma_intr);
446
447 return 0;
448 }
449
450 static int
451 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
452 void (*intr)(void *), void *arg, struct audio_params *params)
453 {
454
455 return ENODEV;
456 }
457
458 static int
459 vidcaudio_halt_output(void *addr)
460 {
461 struct vidcaudio_softc *sc = addr;
462
463 DPRINTF(("vidcaudio_halt_output\n"));
464 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
465 disable_irq(sc->sc_dma_intr);
466 return 0;
467 }
468
469 static int
470 vidcaudio_halt_input(void *addr)
471 {
472
473 return ENODEV;
474 }
475
476 static int
477 vidcaudio_getdev(void *addr, struct audio_device *retp)
478 {
479
480 *retp = vidcaudio_device;
481 return 0;
482 }
483
484
485 static int
486 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
487 {
488
489 return EINVAL;
490 }
491
492 static int
493 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
494 {
495
496 return EINVAL;
497 }
498
499 static int
500 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
501 {
502
503 return ENXIO;
504 }
505
506 static int
507 vidcaudio_get_props(void *addr)
508 {
509
510 return 0;
511 }
512
513 static void
514 vidcaudio_rate(int rate)
515 {
516
517 WriteWord(vidc_base, VIDC_SFR | rate);
518 }
519
520 static void
521 vidcaudio_ctrl(int ctrl)
522 {
523
524 WriteWord(vidc_base, VIDC_SCR | ctrl);
525 }
526
527 static void
528 vidcaudio_stereo(int channel, int position)
529 {
530
531 channel = channel << 24 | VIDC_SIR0;
532 WriteWord(vidc_base, channel | position);
533 }
534
535 static int
536 vidcaudio_intr(void *arg)
537 {
538 struct vidcaudio_softc *sc = arg;
539 int status;
540 paddr_t pnext, pend;
541
542 status = IOMD_READ_BYTE(IOMD_SD0ST);
543 DPRINTF(("I[%x]", status));
544 if ((status & IOMD_DMAST_INT) == 0)
545 return 0;
546
547 /* FIXME: Not really allowed to use pmap here. */
548 pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_pnext, &pnext);
549 pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
550
551 switch (status &
552 (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
553
554 case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
555 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
556 DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
557 IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
558 IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
559 break;
560
561 case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
562 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
563 DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
564 IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
565 IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
566 break;
567 }
568
569 sc->sc_pnext = (char *)sc->sc_pnext + sc->sc_pblksize;
570 if (sc->sc_pnext >= sc->sc_pend)
571 sc->sc_pnext = sc->sc_pstart;
572
573 if (sc->sc_pcountdown > 0)
574 sc->sc_pcountdown--;
575 else
576 (*sc->sc_pintr)(sc->sc_parg);
577
578 return 1;
579 }
580