1 /* $NetBSD: vs.c,v 1.56 2022/05/26 14:33:29 tsutsui 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.56 2022/05/26 14:33:29 tsutsui 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 #include "ioconf.h"
59
60 #ifdef VS_DEBUG
61 #define DPRINTF(y,x) if (vs_debug >= (y)) printf x
62 static int vs_debug;
63 #ifdef AUDIO_DEBUG
64 extern int audiodebug;
65 #endif
66 #else
67 #define DPRINTF(y,x)
68 #endif
69
70 static int vs_match(device_t, cfdata_t, void *);
71 static void vs_attach(device_t, device_t, void *);
72
73 static int vs_dmaintr(void *);
74 static int vs_dmaerrintr(void *);
75
76 /* MI audio layer interface */
77 static int vs_query_format(void *, audio_format_query_t *);
78 static int vs_set_format(void *, int,
79 const audio_params_t *, const audio_params_t *,
80 audio_filter_reg_t *, audio_filter_reg_t *);
81 static int vs_commit_settings(void *);
82 static int vs_start_input(void *, void *, int, void (*)(void *), void *);
83 static int vs_start_output(void *, void *, int, void (*)(void *), void *);
84 static int vs_halt_output(void *);
85 static int vs_halt_input(void *);
86 static int vs_allocmem(struct vs_softc *, size_t, size_t, size_t,
87 struct vs_dma *);
88 static void vs_freemem(struct vs_dma *);
89 static int vs_getdev(void *, struct audio_device *);
90 static int vs_set_port(void *, mixer_ctrl_t *);
91 static int vs_get_port(void *, mixer_ctrl_t *);
92 static int vs_query_devinfo(void *, mixer_devinfo_t *);
93 static void *vs_allocm(void *, int, size_t);
94 static void vs_freem(void *, void *, size_t);
95 static size_t vs_round_buffersize(void *, int, size_t);
96 static int vs_get_props(void *);
97 static void vs_get_locks(void *, kmutex_t **, kmutex_t **);
98
99 /* lower functions */
100 static int vs_round_sr(u_long);
101 static inline void vs_set_panout(struct vs_softc *, u_long);
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
vs_match(device_t parent,cfdata_t cf,void * aux)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
vs_attach(device_t parent,device_t self,void * aux)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_addr = (void *) ia->ia_addr;
216 sc->sc_dmas = NULL;
217 sc->sc_prev_vd = NULL;
218 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
219 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
220
221 /* XXX */
222 bus_space_map(iot, PPI_ADDR, PPI_MAPSIZE, BUS_SPACE_MAP_SHIFTED,
223 &sc->sc_ppi);
224
225 /* Initialize DMAC */
226 sc->sc_dmat = ia->ia_dmat;
227 sc->sc_dma_ch = dmac_alloc_channel(parent, ia->ia_dma, "vs",
228 ia->ia_dmaintr, vs_dmaintr, sc,
229 ia->ia_dmaintr+1, vs_dmaerrintr, sc,
230 (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC | DMAC_DCR_OPS_8BIT),
231 (DMAC_OCR_SIZE_BYTE | DMAC_OCR_REQG_EXTERNAL));
232
233 aprint_normal_dev(self, "MSM6258V ADPCM voice synthesizer\n");
234
235 audio_attach_mi(&vs_hw_if, sc, sc->sc_dev);
236 }
237
238 /*
239 * vs interrupt handler
240 */
241 static int
vs_dmaintr(void * hdl)242 vs_dmaintr(void *hdl)
243 {
244 struct vs_softc *sc;
245
246 DPRINTF(2, ("vs_dmaintr\n"));
247 sc = hdl;
248
249 mutex_spin_enter(&sc->sc_intr_lock);
250
251 if (sc->sc_pintr) {
252 sc->sc_pintr(sc->sc_parg);
253 } else if (sc->sc_rintr) {
254 sc->sc_rintr(sc->sc_rarg);
255 } else {
256 printf("vs_dmaintr: spurious interrupt\n");
257 }
258
259 mutex_spin_exit(&sc->sc_intr_lock);
260
261 return 1;
262 }
263
264 static int
vs_dmaerrintr(void * hdl)265 vs_dmaerrintr(void *hdl)
266 {
267 struct vs_softc *sc;
268
269 sc = hdl;
270 DPRINTF(1, ("%s: DMA transfer error.\n", device_xname(sc->sc_dev)));
271 /* XXX */
272 vs_dmaintr(sc);
273
274 return 1;
275 }
276
277
278 /*
279 * audio MD layer interfaces
280 */
281
282 static int
vs_query_format(void * hdl,audio_format_query_t * afp)283 vs_query_format(void *hdl, audio_format_query_t *afp)
284 {
285
286 return audio_query_format(&vs_formats, 1, afp);
287 }
288
289 static int
vs_round_sr(u_long rate)290 vs_round_sr(u_long rate)
291 {
292 int i;
293
294 for (i = 0; i < NUM_RATE; i++) {
295 if (rate == vs_l2r[i].rate)
296 return i;
297 }
298 return -1;
299 }
300
301 static int
vs_set_format(void * hdl,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)302 vs_set_format(void *hdl, int setmode,
303 const audio_params_t *play, const audio_params_t *rec,
304 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
305 {
306 struct vs_softc *sc;
307 int rate;
308
309 sc = hdl;
310
311 DPRINTF(1, ("%s: mode=%d %s/%dbit/%dch/%dHz: ", __func__,
312 setmode, audio_encoding_name(play->encoding),
313 play->precision, play->channels, play->sample_rate));
314
315 /* *play and *rec are identical because !AUDIO_PROP_INDEPENDENT */
316
317 rate = vs_round_sr(play->sample_rate);
318 KASSERT(rate >= 0);
319 sc->sc_current.rate = rate;
320
321 if ((setmode & AUMODE_PLAY) != 0) {
322 pfil->codec = msm6258_internal_to_adpcm;
323 pfil->context = &sc->sc_codecvar;
324 }
325 if ((setmode & AUMODE_RECORD) != 0) {
326 rfil->codec = msm6258_adpcm_to_internal;
327 rfil->context = &sc->sc_codecvar;
328 }
329
330 DPRINTF(1, ("accepted\n"));
331 return 0;
332 }
333
334 static int
vs_commit_settings(void * hdl)335 vs_commit_settings(void *hdl)
336 {
337 struct vs_softc *sc;
338 int rate;
339
340 sc = hdl;
341 rate = sc->sc_current.rate;
342
343 DPRINTF(1, ("commit_settings: sample rate to %d, %d\n",
344 rate, (int)vs_l2r[rate].rate));
345 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
346 (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
347 PPI_PORTC) & 0xf0)
348 | vs_l2r[rate].den);
349 adpcm_chgclk(vs_l2r[rate].clk);
350
351 return 0;
352 }
353
354 static inline void
vs_set_panout(struct vs_softc * sc,u_long po)355 vs_set_panout(struct vs_softc *sc, u_long po)
356 {
357
358 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
359 (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
360 & 0xfc) | po);
361 }
362
363 static int
vs_start_output(void * hdl,void * block,int blksize,void (* intr)(void *),void * arg)364 vs_start_output(void *hdl, void *block, int blksize, void (*intr)(void *),
365 void *arg)
366 {
367 struct vs_softc *sc;
368 struct vs_dma *vd;
369 struct dmac_channel_stat *chan;
370
371 DPRINTF(2, ("%s: block=%p blksize=%d\n", __func__, block, blksize));
372 sc = hdl;
373
374 /* Find DMA buffer. */
375 for (vd = sc->sc_dmas; vd != NULL; vd = vd->vd_next) {
376 if (KVADDR(vd) <= block && block < KVADDR_END(vd)
377 break;
378 }
379 if (vd == NULL) {
380 printf("%s: start_output: bad addr %p\n",
381 device_xname(sc->sc_dev), block);
382 return EINVAL;
383 }
384
385 chan = sc->sc_dma_ch;
386
387 if (vd != sc->sc_prev_vd) {
388 sc->sc_current.xfer = dmac_prepare_xfer(chan, sc->sc_dmat,
389 vd->vd_map, DMAC_OCR_DIR_MTD,
390 (DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT),
391 sc->sc_addr + MSM6258_DATA * 2 + 1);
392 sc->sc_prev_vd = vd;
393 }
394 dmac_start_xfer_offset(chan->ch_softc, sc->sc_current.xfer,
395 (int)block - (int)KVADDR(vd), blksize);
396
397 if (sc->sc_pintr == NULL) {
398 sc->sc_pintr = intr;
399 sc->sc_parg = arg;
400
401 vs_set_panout(sc, VS_PANOUT_LR);
402 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
403 MSM6258_CMD, MSM6258_CMD_PLAY_START);
404 }
405
406 return 0;
407 }
408
409 static int
410 vs_start_input(void *hdl, void *block, int blksize, void (*intr)(void *),
411 void *arg)
412 {
413 struct vs_softc *sc;
414 struct vs_dma *vd;
415 struct dmac_channel_stat *chan;
416
417 DPRINTF(2, ("%s: block=%p blksize=%d\n", __func__, block, blksize));
418 sc = hdl;
419
420 /* Find DMA buffer. */
421 for (vd = sc->sc_dmas; vd != NULL; vd = vd->vd_next) {
422 if (KVADDR(vd) <= block && block < KVADDR_END(vd)
423 break;
424 }
425 if (vd == NULL) {
426 printf("%s: start_output: bad addr %p\n",
427 device_xname(sc->sc_dev), block);
428 return EINVAL;
429 }
430
431 chan = sc->sc_dma_ch;
432
433 if (vd != sc->sc_prev_vd) {
434 sc->sc_current.xfer = dmac_prepare_xfer(chan, sc->sc_dmat,
435 vd->vd_map, DMAC_OCR_DIR_DTM,
436 (DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT),
437 sc->sc_addr + MSM6258_DATA * 2 + 1);
438 sc->sc_prev_vd = vd;
439 }
440 dmac_start_xfer_offset(chan->ch_softc, sc->sc_current.xfer,
441 (int)block - (int)KVADDR(vd), blksize);
442
443 if (sc->sc_rintr == NULL) {
444 sc->sc_rintr = intr;
445 sc->sc_rarg = arg;
446
447 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
448 MSM6258_CMD, MSM6258_CMD_REC_START);
449 }
450
451 return 0;
452 }
453
454 static int
455 vs_halt_output(void *hdl)
456 {
457 struct vs_softc *sc;
458
459 DPRINTF(1, ("vs_halt_output\n"));
460 sc = hdl;
461
462 /* stop ADPCM play */
463 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
464 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
465 MSM6258_CMD, MSM6258_CMD_STOP);
466 sc->sc_pintr = NULL;
467
468 return 0;
469 }
470
471 static int
472 vs_halt_input(void *hdl)
473 {
474 struct vs_softc *sc;
475
476 DPRINTF(1, ("vs_halt_input\n"));
477 sc = hdl;
478
479 /* stop ADPCM recording */
480 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
481 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
482 MSM6258_CMD, MSM6258_CMD_STOP);
483 sc->sc_rintr = NULL;
484
485 return 0;
486 }
487
488 static int
489 vs_allocmem(struct vs_softc *sc, size_t size, size_t align, size_t boundary,
490 struct vs_dma *vd)
491 {
492 int error;
493
494 #ifdef DIAGNOSTIC
495 if (size > DMAC_MAXSEGSZ)
496 panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
497 #endif
498
499 vd->vd_size = size;
500
501 error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
502 vd->vd_segs,
503 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
504 &vd->vd_nsegs, BUS_DMA_WAITOK);
505 if (error)
506 goto out;
507
508 error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
509 vd->vd_size, &vd->vd_addr,
510 BUS_DMA_WAITOK | BUS_DMA_COHERENT);
511 if (error)
512 goto free;
513
514 error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
515 0, BUS_DMA_WAITOK, &vd->vd_map);
516 if (error)
517 goto unmap;
518
519 error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
520 vd->vd_size, NULL, BUS_DMA_WAITOK);
521 if (error)
522 goto destroy;
523
524 return 0;
525
526 destroy:
527 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
528 unmap:
529 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
530 free:
531 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
532 out:
533 return error;
534 }
535
536 static void
537 vs_freemem(struct vs_dma *vd)
538 {
539
540 bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
541 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
542 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
543 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
544 }
545
546 static int
547 vs_getdev(void *hdl, struct audio_device *retp)
548 {
549
550 DPRINTF(1, ("vs_getdev\n"));
551 *retp = vs_device;
552 return 0;
553 }
554
555 static int
556 vs_set_port(void *hdl, mixer_ctrl_t *cp)
557 {
558
559 DPRINTF(1, ("vs_set_port\n"));
560 return 0;
561 }
562
563 static int
564 vs_get_port(void *hdl, mixer_ctrl_t *cp)
565 {
566
567 DPRINTF(1, ("vs_get_port\n"));
568 return 0;
569 }
570
571 static int
572 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
573 {
574
575 DPRINTF(1, ("vs_query_devinfo\n"));
576 switch (mi->index) {
577 default:
578 return EINVAL;
579 }
580 return 0;
581 }
582
583 static void *
584 vs_allocm(void *hdl, int direction, size_t size)
585 {
586 struct vs_softc *sc;
587 struct vs_dma *vd;
588 int error;
589
590 vd = kmem_alloc(sizeof(*vd), KM_SLEEP);
591 sc = hdl;
592 vd->vd_dmat = sc->sc_dmat;
593
594 error = vs_allocmem(sc, size, 32, 0, vd);
595 if (error) {
596 kmem_free(vd, sizeof(*vd));
597 return NULL;
598 }
599 vd->vd_next = sc->sc_dmas;
600 sc->sc_dmas = vd;
601
602 return KVADDR(vd);
603 }
604
605 static void
606 vs_freem(void *hdl, void *addr, size_t size)
607 {
608 struct vs_softc *sc;
609 struct vs_dma *p, **pp;
610
611 sc = hdl;
612 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
613 if (KVADDR(p) == addr) {
614 vs_freemem(p);
615 *pp = p->vd_next;
616 kmem_free(p, sizeof(*p));
617 return;
618 }
619 }
620 }
621
622 static size_t
623 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
624 {
625
626 if (bufsize > DMAC_MAXSEGSZ)
627 bufsize = DMAC_MAXSEGSZ;
628 return bufsize;
629 }
630
631 static int
632 vs_get_props(void *hdl)
633 {
634
635 DPRINTF(1, ("vs_get_props\n"));
636 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
637 }
638
639 static void
640 vs_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
641 {
642 struct vs_softc *sc;
643
644 DPRINTF(1, ("vs_get_locks\n"));
645 sc = hdl;
646 *intr = &sc->sc_intr_lock;
647 *thread = &sc->sc_lock;
648 }
649
650 #endif /* NAUDIO > 0 && NVS > 0*/
651