vs.c revision 1.53 1 /* $NetBSD: vs.c,v 1.53 2021/02/06 09:27:35 isaki Exp $ */
2
3 /*
4 * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * VS - OKI MSM6258 ADPCM voice synthesizer device driver.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: vs.c,v 1.53 2021/02/06 09:27:35 isaki Exp $");
34
35 #include "audio.h"
36 #include "vs.h"
37 #if NAUDIO > 0 && NVS > 0
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/kmem.h>
43
44 #include <sys/audioio.h>
45 #include <dev/audio/audio_if.h>
46
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49
50 #include <dev/ic/msm6258var.h>
51
52 #include <arch/x68k/dev/dmacvar.h>
53 #include <arch/x68k/dev/intiovar.h>
54 #include <arch/x68k/dev/opmvar.h>
55
56 #include <arch/x68k/dev/vsvar.h>
57
58 #ifdef VS_DEBUG
59 #define DPRINTF(y,x) if (vs_debug >= (y)) printf x
60 static int vs_debug;
61 #ifdef AUDIO_DEBUG
62 extern int audiodebug;
63 #endif
64 #else
65 #define DPRINTF(y,x)
66 #endif
67
68 static int vs_match(device_t, cfdata_t, void *);
69 static void vs_attach(device_t, device_t, void *);
70
71 static int vs_dmaintr(void *);
72 static int vs_dmaerrintr(void *);
73
74 /* MI audio layer interface */
75 static int vs_query_format(void *, audio_format_query_t *);
76 static int vs_set_format(void *, int,
77 const audio_params_t *, const audio_params_t *,
78 audio_filter_reg_t *, audio_filter_reg_t *);
79 static int vs_commit_settings(void *);
80 static int vs_start_input(void *, void *, int, void (*)(void *), void *);
81 static int vs_start_output(void *, void *, int, void (*)(void *), void *);
82 static int vs_halt_output(void *);
83 static int vs_halt_input(void *);
84 static int vs_allocmem(struct vs_softc *, size_t, size_t, size_t,
85 struct vs_dma *);
86 static void vs_freemem(struct vs_dma *);
87 static int vs_getdev(void *, struct audio_device *);
88 static int vs_set_port(void *, mixer_ctrl_t *);
89 static int vs_get_port(void *, mixer_ctrl_t *);
90 static int vs_query_devinfo(void *, mixer_devinfo_t *);
91 static void *vs_allocm(void *, int, size_t);
92 static void vs_freem(void *, void *, size_t);
93 static size_t vs_round_buffersize(void *, int, size_t);
94 static int vs_get_props(void *);
95 static void vs_get_locks(void *, kmutex_t **, kmutex_t **);
96
97 /* lower functions */
98 static int vs_round_sr(u_long);
99 static inline void vs_set_panout(struct vs_softc *, u_long);
100
101 extern struct cfdriver vs_cd;
102
103 CFATTACH_DECL_NEW(vs, sizeof(struct vs_softc),
104 vs_match, vs_attach, NULL, NULL);
105
106 static int vs_attached;
107
108 static const struct audio_hw_if vs_hw_if = {
109 .query_format = vs_query_format,
110 .set_format = vs_set_format,
111 .commit_settings = vs_commit_settings,
112 .start_output = vs_start_output,
113 .start_input = vs_start_input,
114 .halt_output = vs_halt_output,
115 .halt_input = vs_halt_input,
116 .getdev = vs_getdev,
117 .set_port = vs_set_port,
118 .get_port = vs_get_port,
119 .query_devinfo = vs_query_devinfo,
120 .allocm = vs_allocm,
121 .freem = vs_freem,
122 .round_buffersize = vs_round_buffersize,
123 .get_props = vs_get_props,
124 .get_locks = vs_get_locks,
125 };
126
127 static struct audio_device vs_device = {
128 "OKI MSM6258",
129 "",
130 "vs"
131 };
132
133 static const struct audio_format vs_formats = {
134 .mode = AUMODE_PLAY | AUMODE_RECORD,
135 .encoding = AUDIO_ENCODING_ADPCM,
136 .validbits = 4,
137 .precision = 4,
138 .channels = 1,
139 .channel_mask = AUFMT_MONAURAL,
140 .frequency_type = 5,
141 .frequency = { VS_RATE_3K, VS_RATE_5K, VS_RATE_7K,
142 VS_RATE_10K, VS_RATE_15K },
143 };
144
145 struct {
146 u_long rate;
147 u_char clk;
148 u_char den;
149 } vs_l2r[] = {
150 { VS_RATE_15K, VS_CLK_8MHZ, VS_SRATE_512 },
151 { VS_RATE_10K, VS_CLK_8MHZ, VS_SRATE_768 },
152 { VS_RATE_7K, VS_CLK_8MHZ, VS_SRATE_1024},
153 { VS_RATE_5K, VS_CLK_4MHZ, VS_SRATE_768 },
154 { VS_RATE_3K, VS_CLK_4MHZ, VS_SRATE_1024}
155 };
156
157 #define NUM_RATE (sizeof(vs_l2r)/sizeof(vs_l2r[0]))
158
159 static int
160 vs_match(device_t parent, cfdata_t cf, void *aux)
161 {
162 struct intio_attach_args *ia;
163
164 ia = aux;
165 if (strcmp(ia->ia_name, "vs") || vs_attached)
166 return 0;
167
168 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
169 ia->ia_addr = VS_ADDR;
170 if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
171 ia->ia_dma = VS_DMA;
172 if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
173 ia->ia_dmaintr = VS_DMAINTR;
174
175 /* fixed parameters */
176 if (ia->ia_addr != VS_ADDR)
177 return 0;
178 if (ia->ia_dma != VS_DMA)
179 return 0;
180 if (ia->ia_dmaintr != VS_DMAINTR)
181 return 0;
182
183 #ifdef VS_DEBUG
184 vs_debug = 1;
185 #ifdef AUDIO_DEBUG
186 audiodebug = 2;
187 #endif
188 #endif
189
190 return 1;
191 }
192
193 static void
194 vs_attach(device_t parent, device_t self, void *aux)
195 {
196 struct vs_softc *sc;
197 bus_space_tag_t iot;
198 bus_space_handle_t ioh;
199 struct intio_attach_args *ia;
200
201 sc = device_private(self);
202 sc->sc_dev = self;
203 ia = aux;
204 vs_attached = 1;
205
206 printf("\n");
207
208 /* Re-map the I/O space */
209 iot = ia->ia_bst;
210 bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
211
212 /* Initialize sc */
213 sc->sc_iot = iot;
214 sc->sc_ioh = ioh;
215 sc->sc_hw_if = &vs_hw_if;
216 sc->sc_addr = (void *) ia->ia_addr;
217 sc->sc_dmas = NULL;
218 sc->sc_prev_vd = NULL;
219 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
220 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
221
222 /* XXX */
223 bus_space_map(iot, PPI_ADDR, PPI_MAPSIZE, BUS_SPACE_MAP_SHIFTED,
224 &sc->sc_ppi);
225
226 /* Initialize DMAC */
227 sc->sc_dmat = ia->ia_dmat;
228 sc->sc_dma_ch = dmac_alloc_channel(parent, ia->ia_dma, "vs",
229 ia->ia_dmaintr, vs_dmaintr, sc,
230 ia->ia_dmaintr+1, vs_dmaerrintr, sc,
231 (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC | DMAC_DCR_OPS_8BIT),
232 (DMAC_OCR_SIZE_BYTE | DMAC_OCR_REQG_EXTERNAL));
233
234 aprint_normal_dev(self, "MSM6258V ADPCM voice synthesizer\n");
235
236 audio_attach_mi(&vs_hw_if, sc, sc->sc_dev);
237 }
238
239 /*
240 * vs interrupt handler
241 */
242 static int
243 vs_dmaintr(void *hdl)
244 {
245 struct vs_softc *sc;
246
247 DPRINTF(2, ("vs_dmaintr\n"));
248 sc = hdl;
249
250 mutex_spin_enter(&sc->sc_intr_lock);
251
252 if (sc->sc_pintr) {
253 sc->sc_pintr(sc->sc_parg);
254 } else if (sc->sc_rintr) {
255 sc->sc_rintr(sc->sc_rarg);
256 } else {
257 printf("vs_dmaintr: spurious interrupt\n");
258 }
259
260 mutex_spin_exit(&sc->sc_intr_lock);
261
262 return 1;
263 }
264
265 static int
266 vs_dmaerrintr(void *hdl)
267 {
268 struct vs_softc *sc;
269
270 sc = hdl;
271 DPRINTF(1, ("%s: DMA transfer error.\n", device_xname(sc->sc_dev)));
272 /* XXX */
273 vs_dmaintr(sc);
274
275 return 1;
276 }
277
278
279 /*
280 * audio MD layer interfaces
281 */
282
283 static int
284 vs_query_format(void *hdl, audio_format_query_t *afp)
285 {
286
287 return audio_query_format(&vs_formats, 1, afp);
288 }
289
290 static int
291 vs_round_sr(u_long rate)
292 {
293 int i;
294
295 for (i = 0; i < NUM_RATE; i++) {
296 if (rate == vs_l2r[i].rate)
297 return i;
298 }
299 return -1;
300 }
301
302 static int
303 vs_set_format(void *hdl, int setmode,
304 const audio_params_t *play, const audio_params_t *rec,
305 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
306 {
307 struct vs_softc *sc;
308 int rate;
309
310 sc = hdl;
311
312 DPRINTF(1, ("%s: mode=%d %s/%dbit/%dch/%dHz: ", __func__,
313 setmode, audio_encoding_name(play->encoding),
314 play->precision, play->channels, play->sample_rate));
315
316 /* *play and *rec are identical because !AUDIO_PROP_INDEPENDENT */
317
318 rate = vs_round_sr(play->sample_rate);
319 KASSERT(rate >= 0);
320 sc->sc_current.rate = rate;
321
322 if ((setmode & AUMODE_PLAY) != 0) {
323 pfil->codec = msm6258_internal_to_adpcm;
324 pfil->context = &sc->sc_codecvar;
325 }
326 if ((setmode & AUMODE_RECORD) != 0) {
327 rfil->codec = msm6258_adpcm_to_internal;
328 rfil->context = &sc->sc_codecvar;
329 }
330
331 DPRINTF(1, ("accepted\n"));
332 return 0;
333 }
334
335 static int
336 vs_commit_settings(void *hdl)
337 {
338 struct vs_softc *sc;
339 int rate;
340
341 sc = hdl;
342 rate = sc->sc_current.rate;
343
344 DPRINTF(1, ("commit_settings: sample rate to %d, %d\n",
345 rate, (int)vs_l2r[rate].rate));
346 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
347 (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
348 PPI_PORTC) & 0xf0)
349 | vs_l2r[rate].den);
350 adpcm_chgclk(vs_l2r[rate].clk);
351
352 return 0;
353 }
354
355 static inline void
356 vs_set_panout(struct vs_softc *sc, u_long po)
357 {
358
359 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
360 (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
361 & 0xfc) | po);
362 }
363
364 static int
365 vs_start_output(void *hdl, void *block, int blksize, void (*intr)(void *),
366 void *arg)
367 {
368 struct vs_softc *sc;
369 struct vs_dma *vd;
370 struct dmac_channel_stat *chan;
371
372 DPRINTF(2, ("%s: block=%p blksize=%d\n", __func__, block, blksize));
373 sc = hdl;
374
375 /* Find DMA buffer. */
376 for (vd = sc->sc_dmas; vd != NULL; vd = vd->vd_next) {
377 if (KVADDR(vd) <= block && block < KVADDR_END(vd)
378 break;
379 }
380 if (vd == NULL) {
381 printf("%s: start_output: bad addr %p\n",
382 device_xname(sc->sc_dev), block);
383 return EINVAL;
384 }
385
386 chan = sc->sc_dma_ch;
387
388 if (vd != sc->sc_prev_vd) {
389 sc->sc_current.xfer = dmac_prepare_xfer(chan, sc->sc_dmat,
390 vd->vd_map, DMAC_OCR_DIR_MTD,
391 (DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT),
392 sc->sc_addr + MSM6258_DATA * 2 + 1);
393 sc->sc_prev_vd = vd;
394 }
395 dmac_start_xfer_offset(chan->ch_softc, sc->sc_current.xfer,
396 (int)block - (int)KVADDR(vd), blksize);
397
398 if (sc->sc_pintr == NULL) {
399 sc->sc_pintr = intr;
400 sc->sc_parg = arg;
401
402 vs_set_panout(sc, VS_PANOUT_LR);
403 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
404 MSM6258_CMD, MSM6258_CMD_PLAY_START);
405 }
406
407 return 0;
408 }
409
410 static int
411 vs_start_input(void *hdl, void *block, int blksize, void (*intr)(void *),
412 void *arg)
413 {
414 struct vs_softc *sc;
415 struct vs_dma *vd;
416 struct dmac_channel_stat *chan;
417
418 DPRINTF(2, ("%s: block=%p blksize=%d\n", __func__, block, blksize));
419 sc = hdl;
420
421 /* Find DMA buffer. */
422 for (vd = sc->sc_dmas; vd != NULL; vd = vd->vd_next) {
423 if (KVADDR(vd) <= block && block < KVADDR_END(vd)
424 break;
425 }
426 if (vd == NULL) {
427 printf("%s: start_output: bad addr %p\n",
428 device_xname(sc->sc_dev), block);
429 return EINVAL;
430 }
431
432 chan = sc->sc_dma_ch;
433
434 if (vd != sc->sc_prev_vd) {
435 sc->sc_current.xfer = dmac_prepare_xfer(chan, sc->sc_dmat,
436 vd->vd_map, DMAC_OCR_DIR_DTM,
437 (DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT),
438 sc->sc_addr + MSM6258_DATA * 2 + 1);
439 sc->sc_prev_vd = vd;
440 }
441 dmac_start_xfer_offset(chan->ch_softc, sc->sc_current.xfer,
442 (int)block - (int)KVADDR(vd), blksize);
443
444 if (sc->sc_rintr == NULL) {
445 sc->sc_rintr = intr;
446 sc->sc_rarg = arg;
447
448 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
449 MSM6258_CMD, MSM6258_CMD_REC_START);
450 }
451
452 return 0;
453 }
454
455 static int
456 vs_halt_output(void *hdl)
457 {
458 struct vs_softc *sc;
459
460 DPRINTF(1, ("vs_halt_output\n"));
461 sc = hdl;
462
463 /* stop ADPCM play */
464 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
465 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
466 MSM6258_CMD, MSM6258_CMD_STOP);
467 sc->sc_pintr = NULL;
468
469 return 0;
470 }
471
472 static int
473 vs_halt_input(void *hdl)
474 {
475 struct vs_softc *sc;
476
477 DPRINTF(1, ("vs_halt_input\n"));
478 sc = hdl;
479
480 /* stop ADPCM recoding */
481 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
482 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
483 MSM6258_CMD, MSM6258_CMD_STOP);
484 sc->sc_rintr = NULL;
485
486 return 0;
487 }
488
489 static int
490 vs_allocmem(struct vs_softc *sc, size_t size, size_t align, size_t boundary,
491 struct vs_dma *vd)
492 {
493 int error;
494
495 #ifdef DIAGNOSTIC
496 if (size > DMAC_MAXSEGSZ)
497 panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
498 #endif
499
500 vd->vd_size = size;
501
502 error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
503 vd->vd_segs,
504 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
505 &vd->vd_nsegs, BUS_DMA_WAITOK);
506 if (error)
507 goto out;
508
509 error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
510 vd->vd_size, &vd->vd_addr,
511 BUS_DMA_WAITOK | BUS_DMA_COHERENT);
512 if (error)
513 goto free;
514
515 error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
516 0, BUS_DMA_WAITOK, &vd->vd_map);
517 if (error)
518 goto unmap;
519
520 error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
521 vd->vd_size, NULL, BUS_DMA_WAITOK);
522 if (error)
523 goto destroy;
524
525 return 0;
526
527 destroy:
528 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
529 unmap:
530 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
531 free:
532 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
533 out:
534 return error;
535 }
536
537 static void
538 vs_freemem(struct vs_dma *vd)
539 {
540
541 bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
542 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
543 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
544 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
545 }
546
547 static int
548 vs_getdev(void *hdl, struct audio_device *retp)
549 {
550
551 DPRINTF(1, ("vs_getdev\n"));
552 *retp = vs_device;
553 return 0;
554 }
555
556 static int
557 vs_set_port(void *hdl, mixer_ctrl_t *cp)
558 {
559
560 DPRINTF(1, ("vs_set_port\n"));
561 return 0;
562 }
563
564 static int
565 vs_get_port(void *hdl, mixer_ctrl_t *cp)
566 {
567
568 DPRINTF(1, ("vs_get_port\n"));
569 return 0;
570 }
571
572 static int
573 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
574 {
575
576 DPRINTF(1, ("vs_query_devinfo\n"));
577 switch (mi->index) {
578 default:
579 return EINVAL;
580 }
581 return 0;
582 }
583
584 static void *
585 vs_allocm(void *hdl, int direction, size_t size)
586 {
587 struct vs_softc *sc;
588 struct vs_dma *vd;
589 int error;
590
591 vd = kmem_alloc(sizeof(*vd), KM_SLEEP);
592 sc = hdl;
593 vd->vd_dmat = sc->sc_dmat;
594
595 error = vs_allocmem(sc, size, 32, 0, vd);
596 if (error) {
597 kmem_free(vd, sizeof(*vd));
598 return NULL;
599 }
600 vd->vd_next = sc->sc_dmas;
601 sc->sc_dmas = vd;
602
603 return KVADDR(vd);
604 }
605
606 static void
607 vs_freem(void *hdl, void *addr, size_t size)
608 {
609 struct vs_softc *sc;
610 struct vs_dma *p, **pp;
611
612 sc = hdl;
613 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
614 if (KVADDR(p) == addr) {
615 vs_freemem(p);
616 *pp = p->vd_next;
617 kmem_free(p, sizeof(*p));
618 return;
619 }
620 }
621 }
622
623 static size_t
624 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
625 {
626
627 if (bufsize > DMAC_MAXSEGSZ)
628 bufsize = DMAC_MAXSEGSZ;
629 return bufsize;
630 }
631
632 static int
633 vs_get_props(void *hdl)
634 {
635
636 DPRINTF(1, ("vs_get_props\n"));
637 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
638 }
639
640 static void
641 vs_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
642 {
643 struct vs_softc *sc;
644
645 DPRINTF(1, ("vs_get_locks\n"));
646 sc = hdl;
647 *intr = &sc->sc_intr_lock;
648 *thread = &sc->sc_lock;
649 }
650
651 #endif /* NAUDIO > 0 && NVS > 0*/
652