vidcaudio.c revision 1.59.4.1 1 /* $NetBSD: vidcaudio.c,v 1.59.4.1 2020/02/29 20:18:19 ad 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.59.4.1 2020/02/29 20:18:19 ad 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/audio_if.h>
81 #include <dev/audio/audiobellvar.h>
82 #include <dev/audio/mulaw.h>
83
84 #include <machine/intr.h>
85 #include <machine/machdep.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
93 #include "pckbd.h"
94 #if NPCKBD > 0
95 #include <dev/pckbport/pckbdvar.h>
96 #endif
97
98 extern int *vidc_base;
99
100 #ifdef VIDCAUDIO_DEBUG
101 #define DPRINTF(x) printf x
102 #else
103 #define DPRINTF(x)
104 #endif
105
106 #define WriteWord(a, b) \
107 *((volatile unsigned int *)(a)) = (b)
108
109 #define ReadWord(a) \
110 (*((volatile unsigned int *)(a)))
111
112 struct vidcaudio_softc {
113 device_t sc_dev;
114
115 irqhandler_t sc_ih;
116 int sc_dma_intr;
117
118 int sc_is16bit;
119
120 size_t sc_pblksize;
121 vaddr_t sc_poffset;
122 vaddr_t sc_pbufsize;
123 paddr_t *sc_ppages;
124 void (*sc_pintr)(void *);
125 void *sc_parg;
126 int sc_pcountdown;
127
128 kmutex_t sc_lock;
129 kmutex_t sc_intr_lock;
130 };
131
132 static int vidcaudio_probe(device_t , cfdata_t , void *);
133 static void vidcaudio_attach(device_t , device_t , void *);
134 static void vidcaudio_close(void *);
135
136 static int vidcaudio_intr(void *);
137 static void vidcaudio_rate(int);
138 static void vidcaudio_ctrl(int);
139 static void vidcaudio_stereo(int, int);
140
141 CFATTACH_DECL_NEW(vidcaudio, sizeof(struct vidcaudio_softc),
142 vidcaudio_probe, vidcaudio_attach, NULL, NULL);
143
144 static int vidcaudio_query_format(void *, audio_format_query_t *);
145 static int vidcaudio_set_format(void *, int,
146 const audio_params_t *, const audio_params_t *,
147 audio_filter_reg_t *, audio_filter_reg_t *);
148 static int vidcaudio_round_blocksize(void *, int, int, const audio_params_t *);
149 static int vidcaudio_trigger_output(void *, void *, void *, int,
150 void (*)(void *), void *, const audio_params_t *);
151 static int vidcaudio_halt_output(void *);
152 static int vidcaudio_getdev(void *, struct audio_device *);
153 static int vidcaudio_set_port(void *, mixer_ctrl_t *);
154 static int vidcaudio_get_port(void *, mixer_ctrl_t *);
155 static int vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
156 static int vidcaudio_get_props(void *);
157 static void vidcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
158
159 static struct audio_device vidcaudio_device = {
160 "ARM VIDC",
161 "",
162 "vidcaudio"
163 };
164
165 static const struct audio_hw_if vidcaudio_hw_if = {
166 .close = vidcaudio_close,
167 .query_format = vidcaudio_query_format,
168 .set_format = vidcaudio_set_format,
169 .round_blocksize = vidcaudio_round_blocksize,
170 .halt_output = vidcaudio_halt_output,
171 .getdev = vidcaudio_getdev,
172 .set_port = vidcaudio_set_port,
173 .get_port = vidcaudio_get_port,
174 .query_devinfo = vidcaudio_query_devinfo,
175 .get_props = vidcaudio_get_props,
176 .trigger_output = vidcaudio_trigger_output,
177 .get_locks = vidcaudio_get_locks,
178 };
179
180 static const struct audio_format vidcaudio_formats_16bit = {
181 .mode = AUMODE_PLAY,
182 .encoding = AUDIO_ENCODING_SLINEAR_LE,
183 .validbits = 16,
184 .precision = 16,
185 .channels = 2,
186 .channel_mask = AUFMT_STEREO,
187 /* There are more selectable frequencies but these should be enough. */
188 .frequency_type = 6,
189 .frequency = {
190 19600, /* /9 */
191 22050, /* /8 */
192 25200, /* /7 */
193 29400, /* /6 */
194 35280, /* /5 */
195 44100, /* /4 */
196 },
197 };
198 static const struct audio_format vidcaudio_formats_8bit = {
199 .mode = AUMODE_PLAY,
200 .encoding = AUDIO_ENCODING_ULAW,
201 .validbits = 8,
202 .precision = 8,
203 .channels = 2, /* we use stereo always */
204 .channel_mask = AUFMT_STEREO,
205 /* frequency is preferably an integer. */
206 .frequency_type = 6,
207 .frequency = {
208 10000, /* 50us */
209 12500, /* 40us */
210 20000, /* 25us */
211 25000, /* 20us */
212 31250, /* 16us */
213 50000, /* 10us */
214 },
215 };
216
217 static int
218 vidcaudio_probe(device_t parent, cfdata_t cf, void *aux)
219 {
220 int id;
221
222 id = IOMD_ID;
223
224 /* So far I only know about this IOMD */
225 switch (id) {
226 case RPC600_IOMD_ID:
227 case ARM7500_IOC_ID:
228 case ARM7500FE_IOC_ID:
229 return 1;
230 default:
231 aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
232 return 0;
233 }
234 }
235
236
237 static void
238 vidcaudio_attach(device_t parent, device_t self, void *aux)
239 {
240 struct vidcaudio_softc *sc = device_private(self);
241 device_t beepdev;
242
243 sc->sc_dev = self;
244 switch (IOMD_ID) {
245 #ifndef EB7500ATX
246 case RPC600_IOMD_ID:
247 sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
248 sc->sc_dma_intr = IRQ_DMASCH0;
249 break;
250 #endif
251 case ARM7500_IOC_ID:
252 case ARM7500FE_IOC_ID:
253 sc->sc_is16bit = true;
254 sc->sc_dma_intr = IRQ_SDMA;
255 break;
256 default:
257 aprint_error(": strange IOMD\n");
258 return;
259 }
260
261 if (sc->sc_is16bit)
262 aprint_normal(": 16-bit external DAC\n");
263 else
264 aprint_normal(": 8-bit internal DAC\n");
265
266 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
267 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
268
269 /* Install the irq handler for the DMA interrupt */
270 sc->sc_ih.ih_func = vidcaudio_intr;
271 sc->sc_ih.ih_arg = sc;
272 sc->sc_ih.ih_level = IPL_AUDIO;
273 sc->sc_ih.ih_name = device_xname(self);
274
275 if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
276 aprint_error("%s: couldn't claim IRQ %d\n",
277 device_xname(self), sc->sc_dma_intr);
278 return;
279 }
280
281 disable_irq(sc->sc_dma_intr);
282
283 beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
284 #if NPCKBD > 0
285 pckbd_hookup_bell(audiobell, beepdev);
286 #endif
287 }
288
289 static void
290 vidcaudio_close(void *addr)
291 {
292 struct vidcaudio_softc *sc;
293
294 DPRINTF(("DEBUG: vidcaudio_close called\n"));
295 sc = addr;
296 /*
297 * We do this here rather than in vidcaudio_halt_output()
298 * because the latter can be called from interrupt context
299 * (audio_pint()->audio_clear()->vidcaudio_halt_output()).
300 */
301 if (sc->sc_ppages != NULL) {
302 free(sc->sc_ppages, M_DEVBUF);
303 sc->sc_ppages = NULL;
304 }
305 }
306
307 /*
308 * Interface to the generic audio driver
309 */
310
311 static int
312 vidcaudio_query_format(void *addr, audio_format_query_t *afp)
313 {
314 struct vidcaudio_softc *sc;
315
316 sc = addr;
317 if (sc->sc_is16bit)
318 return audio_query_format(&vidcaudio_formats_16bit, 1, afp);
319 else
320 return audio_query_format(&vidcaudio_formats_8bit, 1, afp);
321 }
322
323 static int
324 vidcaudio_set_format(void *addr, int setmode,
325 const audio_params_t *play, const audio_params_t *rec,
326 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
327 {
328 struct vidcaudio_softc *sc;
329 int sample_period, ch;
330
331 sc = addr;
332 if (sc->sc_is16bit) {
333 /* ARM7500ish, 16-bit, two-channel */
334 sample_period = 705600 / 4 / play->sample_rate;
335 if (sample_period < 3)
336 sample_period = 3;
337 vidcaudio_rate(sample_period - 2);
338 vidcaudio_ctrl(SCR_SERIAL);
339 } else {
340 /* VIDC20ish, u-law, 8-channel */
341 /*
342 * We always use two hardware channels, because using
343 * one at 8kHz gives a nasty whining sound from the
344 * speaker.
345 */
346 sample_period = 1000000 / 2 / play->sample_rate;
347 if (sample_period < 3)
348 sample_period = 3;
349 vidcaudio_rate(sample_period - 2);
350 vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
351 for (ch = 0; ch < 8; ch += 2)
352 vidcaudio_stereo(ch, SIR_LEFT_100);
353 for (ch = 1; ch < 8; ch += 2)
354 vidcaudio_stereo(ch, SIR_RIGHT_100);
355
356 pfil->codec = audio_internal_to_mulaw;
357 }
358 return 0;
359 }
360
361 static int
362 vidcaudio_round_blocksize(void *addr, int wantblk,
363 int mode, const audio_params_t *param)
364 {
365 int blk;
366
367 /*
368 * Find the smallest power of two that's larger than the
369 * requested block size, but don't allow < 32 (DMA burst is 16
370 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
371 * confined to a page).
372 */
373
374 for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
375 if (blk >= wantblk)
376 return blk;
377 return blk;
378 }
379
380 static int
381 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
382 void (*intr)(void *), void *arg, const audio_params_t *params)
383 {
384 struct vidcaudio_softc *sc;
385 size_t npages, i;
386
387 DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
388 start, end, blksize));
389
390 sc = addr;
391 KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize, 0, NULL));
392 KASSERT((vaddr_t)start % blksize == 0);
393
394 sc->sc_pblksize = blksize;
395 sc->sc_pbufsize = (char *)end - (char *)start;
396 npages = sc->sc_pbufsize >> PGSHIFT;
397 if (sc->sc_ppages != NULL)
398 free(sc->sc_ppages, M_DEVBUF);
399 sc->sc_ppages = malloc(npages * sizeof(paddr_t), M_DEVBUF, M_WAITOK);
400 if (sc->sc_ppages == NULL)
401 return ENOMEM;
402 for (i = 0; i < npages; i++)
403 if (!pmap_extract(pmap_kernel(),
404 (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
405 return EIO;
406 sc->sc_poffset = 0;
407 sc->sc_pintr = intr;
408 sc->sc_parg = arg;
409
410 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
411 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
412
413 /*
414 * Queue up the first two blocks, but don't tell audio code
415 * we're finished with them yet.
416 */
417 sc->sc_pcountdown = 2;
418
419 enable_irq(sc->sc_dma_intr);
420
421 return 0;
422 }
423
424 static int
425 vidcaudio_halt_output(void *addr)
426 {
427 struct vidcaudio_softc *sc;
428
429 DPRINTF(("vidcaudio_halt_output\n"));
430 sc = addr;
431 disable_irq(sc->sc_dma_intr);
432 IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
433 return 0;
434 }
435
436 static int
437 vidcaudio_getdev(void *addr, struct audio_device *retp)
438 {
439
440 *retp = vidcaudio_device;
441 return 0;
442 }
443
444
445 static int
446 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
447 {
448
449 return EINVAL;
450 }
451
452 static int
453 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
454 {
455
456 return EINVAL;
457 }
458
459 static int
460 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
461 {
462
463 return ENXIO;
464 }
465
466 static int
467 vidcaudio_get_props(void *addr)
468 {
469
470 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
471 }
472
473 static void
474 vidcaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
475 {
476 struct vidcaudio_softc *sc = opaque;
477
478 *intr = &sc->sc_intr_lock;
479 *thread = &sc->sc_lock;
480 }
481
482 static void
483 vidcaudio_rate(int rate)
484 {
485
486 WriteWord(vidc_base, VIDC_SFR | rate);
487 }
488
489 static void
490 vidcaudio_ctrl(int ctrl)
491 {
492
493 WriteWord(vidc_base, VIDC_SCR | ctrl);
494 }
495
496 static void
497 vidcaudio_stereo(int channel, int position)
498 {
499
500 channel = channel << 24 | VIDC_SIR0;
501 WriteWord(vidc_base, channel | position);
502 }
503
504 static int
505 vidcaudio_intr(void *arg)
506 {
507 struct vidcaudio_softc *sc;
508 int status;
509 paddr_t pnext, pend;
510
511 sc = arg;
512 mutex_spin_enter(&sc->sc_intr_lock);
513
514 status = IOMD_READ_BYTE(IOMD_SD0ST);
515 DPRINTF(("I[%x]", status));
516 if ((status & IOMD_DMAST_INT) == 0) {
517 mutex_spin_exit(&sc->sc_intr_lock);
518 return 0;
519 }
520
521 pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
522 (sc->sc_poffset & PGOFSET);
523 pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
524
525 switch (status &
526 (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
527
528 case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
529 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
530 DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
531 IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
532 IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
533 break;
534
535 case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
536 case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
537 DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
538 IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
539 IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
540 break;
541 }
542
543 sc->sc_poffset += sc->sc_pblksize;
544 if (sc->sc_poffset >= sc->sc_pbufsize)
545 sc->sc_poffset = 0;
546
547 if (sc->sc_pcountdown > 0)
548 sc->sc_pcountdown--;
549 else
550 (*sc->sc_pintr)(sc->sc_parg);
551
552 mutex_spin_exit(&sc->sc_intr_lock);
553 return 1;
554 }
555