fms.c revision 1.38 1 /* $NetBSD: fms.c,v 1.38 2010/11/13 13:52:05 uebayasi Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Witold J. Wnuk.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Forte Media FM801 Audio Device Driver
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: fms.c,v 1.38 2010/11/13 13:52:05 uebayasi Exp $");
38
39 #include "mpu.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/audioio.h>
47
48 #include <sys/bus.h>
49 #include <sys/cpu.h>
50
51 #include <dev/pci/pcidevs.h>
52 #include <dev/pci/pcivar.h>
53
54 #include <dev/audio_if.h>
55 #include <dev/mulaw.h>
56 #include <dev/auconv.h>
57
58 #include <dev/ic/ac97var.h>
59 #include <dev/ic/mpuvar.h>
60
61 #include <dev/pci/fmsvar.h>
62
63
64 struct fms_dma {
65 struct fms_dma *next;
66 void *addr;
67 size_t size;
68 bus_dmamap_t map;
69 bus_dma_segment_t seg;
70 };
71
72
73
74 static int fms_match(device_t, cfdata_t, void *);
75 static void fms_attach(device_t, device_t, void *);
76 static int fms_intr(void *);
77
78 static int fms_query_encoding(void *, struct audio_encoding *);
79 static int fms_set_params(void *, int, int, audio_params_t *,
80 audio_params_t *, stream_filter_list_t *,
81 stream_filter_list_t *);
82 static int fms_round_blocksize(void *, int, int, const audio_params_t *);
83 static int fms_halt_output(void *);
84 static int fms_halt_input(void *);
85 static int fms_getdev(void *, struct audio_device *);
86 static int fms_set_port(void *, mixer_ctrl_t *);
87 static int fms_get_port(void *, mixer_ctrl_t *);
88 static int fms_query_devinfo(void *, mixer_devinfo_t *);
89 static void *fms_malloc(void *, int, size_t, struct malloc_type *, int);
90 static void fms_free(void *, void *, struct malloc_type *);
91 static size_t fms_round_buffersize(void *, int, size_t);
92 static paddr_t fms_mappage(void *, void *, off_t, int);
93 static int fms_get_props(void *);
94 static int fms_trigger_output(void *, void *, void *, int,
95 void (*)(void *), void *,
96 const audio_params_t *);
97 static int fms_trigger_input(void *, void *, void *, int,
98 void (*)(void *), void *,
99 const audio_params_t *);
100
101 CFATTACH_DECL(fms, sizeof (struct fms_softc),
102 fms_match, fms_attach, NULL, NULL);
103
104 static struct audio_device fms_device = {
105 "Forte Media 801",
106 "1.0",
107 "fms"
108 };
109
110
111 static const struct audio_hw_if fms_hw_if = {
112 NULL, /* open */
113 NULL, /* close */
114 NULL,
115 fms_query_encoding,
116 fms_set_params,
117 fms_round_blocksize,
118 NULL,
119 NULL,
120 NULL,
121 NULL,
122 NULL,
123 fms_halt_output,
124 fms_halt_input,
125 NULL,
126 fms_getdev,
127 NULL,
128 fms_set_port,
129 fms_get_port,
130 fms_query_devinfo,
131 fms_malloc,
132 fms_free,
133 fms_round_buffersize,
134 fms_mappage,
135 fms_get_props,
136 fms_trigger_output,
137 fms_trigger_input,
138 NULL,
139 NULL,
140 };
141
142 static int fms_attach_codec(void *, struct ac97_codec_if *);
143 static int fms_read_codec(void *, uint8_t, uint16_t *);
144 static int fms_write_codec(void *, uint8_t, uint16_t);
145 static int fms_reset_codec(void *);
146
147 #define FM_PCM_VOLUME 0x00
148 #define FM_FM_VOLUME 0x02
149 #define FM_I2S_VOLUME 0x04
150 #define FM_RECORD_SOURCE 0x06
151
152 #define FM_PLAY_CTL 0x08
153 #define FM_PLAY_RATE_MASK 0x0f00
154 #define FM_PLAY_BUF1_LAST 0x0001
155 #define FM_PLAY_BUF2_LAST 0x0002
156 #define FM_PLAY_START 0x0020
157 #define FM_PLAY_PAUSE 0x0040
158 #define FM_PLAY_STOPNOW 0x0080
159 #define FM_PLAY_16BIT 0x4000
160 #define FM_PLAY_STEREO 0x8000
161
162 #define FM_PLAY_DMALEN 0x0a
163 #define FM_PLAY_DMABUF1 0x0c
164 #define FM_PLAY_DMABUF2 0x10
165
166
167 #define FM_REC_CTL 0x14
168 #define FM_REC_RATE_MASK 0x0f00
169 #define FM_REC_BUF1_LAST 0x0001
170 #define FM_REC_BUF2_LAST 0x0002
171 #define FM_REC_START 0x0020
172 #define FM_REC_PAUSE 0x0040
173 #define FM_REC_STOPNOW 0x0080
174 #define FM_REC_16BIT 0x4000
175 #define FM_REC_STEREO 0x8000
176
177
178 #define FM_REC_DMALEN 0x16
179 #define FM_REC_DMABUF1 0x18
180 #define FM_REC_DMABUF2 0x1c
181
182 #define FM_CODEC_CTL 0x22
183 #define FM_VOLUME 0x26
184 #define FM_VOLUME_MUTE 0x8000
185
186 #define FM_CODEC_CMD 0x2a
187 #define FM_CODEC_CMD_READ 0x0080
188 #define FM_CODEC_CMD_VALID 0x0100
189 #define FM_CODEC_CMD_BUSY 0x0200
190
191 #define FM_CODEC_DATA 0x2c
192
193 #define FM_IO_CTL 0x52
194 #define FM_CARD_CTL 0x54
195
196 #define FM_INTMASK 0x56
197 #define FM_INTMASK_PLAY 0x0001
198 #define FM_INTMASK_REC 0x0002
199 #define FM_INTMASK_VOL 0x0040
200 #define FM_INTMASK_MPU 0x0080
201
202 #define FM_INTSTATUS 0x5a
203 #define FM_INTSTATUS_PLAY 0x0100
204 #define FM_INTSTATUS_REC 0x0200
205 #define FM_INTSTATUS_VOL 0x4000
206 #define FM_INTSTATUS_MPU 0x8000
207
208
209 static int
210 fms_match(device_t parent, cfdata_t match, void *aux)
211 {
212 struct pci_attach_args *pa;
213
214 pa = (struct pci_attach_args *)aux;
215 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA)
216 return 0;
217 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801)
218 return 0;
219
220 return 1;
221 }
222
223 static void
224 fms_attach(device_t parent, device_t self, void *aux)
225 {
226 struct pci_attach_args *pa;
227 struct fms_softc *sc;
228 struct audio_attach_args aa;
229 const char *intrstr;
230 pci_chipset_tag_t pc;
231 pcitag_t pt;
232 pci_intr_handle_t ih;
233 uint16_t k1;
234
235 pa = aux;
236 sc = device_private(self);
237 intrstr = NULL;
238 pc = pa->pa_pc;
239 pt = pa->pa_tag;
240 aprint_naive(": Audio controller\n");
241 aprint_normal(": Forte Media FM-801\n");
242
243 if (pci_intr_map(pa, &ih)) {
244 aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
245 return;
246 }
247 intrstr = pci_intr_string(pc, ih);
248
249 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc);
250 if (sc->sc_ih == NULL) {
251 aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
252 if (intrstr != NULL)
253 aprint_error(" at %s", intrstr);
254 aprint_error("\n");
255 return;
256 }
257
258 sc->sc_dmat = pa->pa_dmat;
259
260 aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
261
262 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
263 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
264 aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
265 return;
266 }
267
268 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
269 &sc->sc_mpu_ioh))
270 panic("fms_attach: can't get mpu subregion handle");
271
272 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
273 &sc->sc_opl_ioh))
274 panic("fms_attach: can't get opl subregion handle");
275
276 /* Disable legacy audio (SBPro compatibility) */
277 pci_conf_write(pc, pt, 0x40, 0);
278
279 /* Reset codec and AC'97 */
280 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
281 delay(2); /* > 1us according to AC'97 documentation */
282 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
283 delay(1); /* > 168.2ns according to AC'97 documentation */
284
285 /* Set up volume */
286 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
287 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
288 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
289
290 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
291
292 /* Unmask playback, record and mpu interrupts, mask the rest */
293 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
294 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
295 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
296 FM_INTMASK_VOL);
297 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
298 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
299 FM_INTSTATUS_VOL);
300
301 sc->host_if.arg = sc;
302 sc->host_if.attach = fms_attach_codec;
303 sc->host_if.read = fms_read_codec;
304 sc->host_if.write = fms_write_codec;
305 sc->host_if.reset = fms_reset_codec;
306
307 if (ac97_attach(&sc->host_if, self) != 0)
308 return;
309
310 audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev);
311
312 aa.type = AUDIODEV_TYPE_OPL;
313 aa.hwif = NULL;
314 aa.hdl = NULL;
315 config_found(&sc->sc_dev, &aa, audioprint);
316
317 aa.type = AUDIODEV_TYPE_MPU;
318 aa.hwif = NULL;
319 aa.hdl = NULL;
320 sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint);
321 }
322
323 /*
324 * Each AC-link frame takes 20.8us, data should be ready in next frame,
325 * we allow more than two.
326 */
327 #define TIMO 50
328 static int
329 fms_read_codec(void *addr, uint8_t reg, uint16_t *val)
330 {
331 struct fms_softc *sc;
332 int i;
333
334 sc = addr;
335 /* Poll until codec is ready */
336 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
337 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
338 delay(1);
339 if (i >= TIMO) {
340 printf("fms: codec busy\n");
341 return 1;
342 }
343
344 /* Write register index, read access */
345 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
346 reg | FM_CODEC_CMD_READ);
347
348 /* Poll until we have valid data */
349 for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
350 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
351 delay(1);
352 if (i >= TIMO) {
353 printf("fms: no data from codec\n");
354 return 1;
355 }
356
357 /* Read data */
358 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
359 return 0;
360 }
361
362 static int
363 fms_write_codec(void *addr, uint8_t reg, uint16_t val)
364 {
365 struct fms_softc *sc = addr;
366 int i;
367
368 /* Poll until codec is ready */
369 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
370 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
371 delay(1);
372 if (i >= TIMO) {
373 printf("fms: codec busy\n");
374 return 1;
375 }
376
377 /* Write data */
378 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
379 /* Write index register, write access */
380 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
381 return 0;
382 }
383 #undef TIMO
384
385 static int
386 fms_attach_codec(void *addr, struct ac97_codec_if *cif)
387 {
388 struct fms_softc *sc;
389
390 sc = addr;
391 sc->codec_if = cif;
392 return 0;
393 }
394
395 /* Cold Reset */
396 static int
397 fms_reset_codec(void *addr)
398 {
399 struct fms_softc *sc;
400
401 sc = addr;
402 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
403 delay(2);
404 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
405 delay(1);
406 return 0;
407 }
408
409 static int
410 fms_intr(void *arg)
411 {
412 struct fms_softc *sc = arg;
413 #if NMPU > 0
414 struct mpu_softc *sc_mpu = device_private(sc->sc_mpu_dev);
415 #endif
416 uint16_t istat;
417
418 istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
419
420 if (istat & FM_INTSTATUS_PLAY) {
421 if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
422 sc->sc_play_end)
423 sc->sc_play_nextblk = sc->sc_play_start;
424
425 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
426 sc->sc_play_flip++ & 1 ?
427 FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
428
429 if (sc->sc_pintr)
430 sc->sc_pintr(sc->sc_parg);
431 else
432 printf("unexpected play intr\n");
433 }
434
435 if (istat & FM_INTSTATUS_REC) {
436 if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
437 sc->sc_rec_end)
438 sc->sc_rec_nextblk = sc->sc_rec_start;
439
440 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
441 sc->sc_rec_flip++ & 1 ?
442 FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
443
444 if (sc->sc_rintr)
445 sc->sc_rintr(sc->sc_rarg);
446 else
447 printf("unexpected rec intr\n");
448 }
449
450 #if NMPU > 0
451 if (istat & FM_INTSTATUS_MPU)
452 mpu_intr(sc_mpu);
453 #endif
454
455 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
456 istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
457
458 return 1;
459 }
460
461 static int
462 fms_query_encoding(void *addr, struct audio_encoding *fp)
463 {
464
465 switch (fp->index) {
466 case 0:
467 strcpy(fp->name, AudioEmulaw);
468 fp->encoding = AUDIO_ENCODING_ULAW;
469 fp->precision = 8;
470 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
471 return 0;
472 case 1:
473 strcpy(fp->name, AudioEslinear_le);
474 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
475 fp->precision = 16;
476 fp->flags = 0;
477 return 0;
478 case 2:
479 strcpy(fp->name, AudioEulinear);
480 fp->encoding = AUDIO_ENCODING_ULINEAR;
481 fp->precision = 8;
482 fp->flags = 0;
483 return 0;
484 case 3:
485 strcpy(fp->name, AudioEalaw);
486 fp->encoding = AUDIO_ENCODING_ALAW;
487 fp->precision = 8;
488 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
489 return 0;
490 case 4:
491 strcpy(fp->name, AudioEulinear_le);
492 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
493 fp->precision = 16;
494 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
495 return 0;
496 case 5:
497 strcpy(fp->name, AudioEslinear);
498 fp->encoding = AUDIO_ENCODING_SLINEAR;
499 fp->precision = 8;
500 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
501 return 0;
502 case 6:
503 strcpy(fp->name, AudioEulinear_be);
504 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
505 fp->precision = 16;
506 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
507 return 0;
508 case 7:
509 strcpy(fp->name, AudioEslinear_be);
510 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
511 fp->precision = 16;
512 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
513 return 0;
514 default:
515 return EINVAL;
516 }
517 }
518
519 /*
520 * Range below -limit- is set to -rate-
521 * What a pity FM801 does not have 24000
522 * 24000 -> 22050 sounds rather poor
523 */
524 static struct {
525 int limit;
526 int rate;
527 } const fms_rates[11] = {
528 { 6600, 5500 },
529 { 8750, 8000 },
530 { 10250, 9600 },
531 { 13200, 11025 },
532 { 17500, 16000 },
533 { 20500, 19200 },
534 { 26500, 22050 },
535 { 35000, 32000 },
536 { 41000, 38400 },
537 { 46000, 44100 },
538 { 48000, 48000 },
539 /* anything above -> 48000 */
540 };
541
542 #define FMS_NFORMATS 4
543 static const struct audio_format fms_formats[FMS_NFORMATS] = {
544 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
545 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
546 32000, 38400, 44100, 48000}},
547 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
548 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
549 32000, 38400, 44100, 48000}},
550 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
551 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
552 32000, 38400, 44100, 48000}},
553 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
554 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
555 32000, 38400, 44100, 48000}},
556 };
557
558 static int
559 fms_set_params(void *addr, int setmode, int usemode,
560 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
561 stream_filter_list_t *rfil)
562 {
563 struct fms_softc *sc;
564 int i, index;
565
566 sc = addr;
567 if (setmode & AUMODE_PLAY) {
568 for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
569 i++)
570 continue;
571 play->sample_rate = fms_rates[i].rate;
572 index = auconv_set_converter(fms_formats, FMS_NFORMATS,
573 AUMODE_PLAY, play, FALSE, pfil);
574 if (index < 0)
575 return EINVAL;
576 sc->sc_play_reg = i << 8;
577 if (fms_formats[index].channels == 2)
578 sc->sc_play_reg |= FM_PLAY_STEREO;
579 if (fms_formats[index].precision == 16)
580 sc->sc_play_reg |= FM_PLAY_16BIT;
581 }
582
583 if (setmode & AUMODE_RECORD) {
584 for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
585 i++)
586 continue;
587 rec->sample_rate = fms_rates[i].rate;
588 index = auconv_set_converter(fms_formats, FMS_NFORMATS,
589 AUMODE_RECORD, rec, FALSE, rfil);
590 if (index < 0)
591 return EINVAL;
592 sc->sc_rec_reg = i << 8;
593 if (fms_formats[index].channels == 2)
594 sc->sc_rec_reg |= FM_REC_STEREO;
595 if (fms_formats[index].precision == 16)
596 sc->sc_rec_reg |= FM_REC_16BIT;
597 }
598
599 return 0;
600 }
601
602 static int
603 fms_round_blocksize(void *addr, int blk, int mode,
604 const audio_params_t *param)
605 {
606
607 return blk & ~0xf;
608 }
609
610 static int
611 fms_halt_output(void *addr)
612 {
613 struct fms_softc *sc;
614 uint16_t k1;
615
616 sc = addr;
617 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
618 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
619 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
620 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
621
622 return 0;
623 }
624
625 static int
626 fms_halt_input(void *addr)
627 {
628 struct fms_softc *sc;
629 uint16_t k1;
630
631 sc = addr;
632 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
633 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
634 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
635 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
636
637 return 0;
638 }
639
640 static int
641 fms_getdev(void *addr, struct audio_device *retp)
642 {
643
644 *retp = fms_device;
645 return 0;
646 }
647
648 static int
649 fms_set_port(void *addr, mixer_ctrl_t *cp)
650 {
651 struct fms_softc *sc;
652
653 sc = addr;
654 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
655 }
656
657 static int
658 fms_get_port(void *addr, mixer_ctrl_t *cp)
659 {
660 struct fms_softc *sc;
661
662 sc = addr;
663 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
664 }
665
666 static void *
667 fms_malloc(void *addr, int direction, size_t size,
668 struct malloc_type *pool, int flags)
669 {
670 struct fms_softc *sc;
671 struct fms_dma *p;
672 int error;
673 int rseg;
674
675 sc = addr;
676 p = malloc(sizeof(*p), pool, flags);
677 if (p == NULL)
678 return NULL;
679
680 p->size = size;
681 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
682 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
683 aprint_error_dev(&sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
684 goto fail_alloc;
685 }
686
687 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
688 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
689 aprint_error_dev(&sc->sc_dev, "unable to map DMA, error = %d\n",
690 error);
691 goto fail_map;
692 }
693
694 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
695 BUS_DMA_NOWAIT, &p->map)) != 0) {
696 aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error = %d\n",
697 error);
698 goto fail_create;
699 }
700
701 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
702 BUS_DMA_NOWAIT)) != 0) {
703 aprint_error_dev(&sc->sc_dev, "unable to load DMA map, error = %d\n",
704 error);
705 goto fail_load;
706 }
707
708 p->next = sc->sc_dmas;
709 sc->sc_dmas = p;
710
711 return p->addr;
712
713
714 fail_load:
715 bus_dmamap_destroy(sc->sc_dmat, p->map);
716 fail_create:
717 bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
718 fail_map:
719 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
720 fail_alloc:
721 free(p, pool);
722 return NULL;
723 }
724
725 static void
726 fms_free(void *addr, void *ptr, struct malloc_type *pool)
727 {
728 struct fms_softc *sc;
729 struct fms_dma **pp, *p;
730
731 sc = addr;
732 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
733 if (p->addr == ptr) {
734 bus_dmamap_unload(sc->sc_dmat, p->map);
735 bus_dmamap_destroy(sc->sc_dmat, p->map);
736 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
737 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
738
739 *pp = p->next;
740 free(p, pool);
741 return;
742 }
743
744 panic("fms_free: trying to free unallocated memory");
745 }
746
747 static size_t
748 fms_round_buffersize(void *addr, int direction, size_t size)
749 {
750
751 return size;
752 }
753
754 static paddr_t
755 fms_mappage(void *addr, void *mem, off_t off, int prot)
756 {
757 struct fms_softc *sc;
758 struct fms_dma *p;
759
760 sc = addr;
761 if (off < 0)
762 return -1;
763
764 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
765 continue;
766 if (p == NULL)
767 return -1;
768
769 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
770 BUS_DMA_WAITOK);
771 }
772
773 static int
774 fms_get_props(void *addr)
775 {
776 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
777 AUDIO_PROP_FULLDUPLEX;
778 }
779
780 static int
781 fms_query_devinfo(void *addr, mixer_devinfo_t *dip)
782 {
783 struct fms_softc *sc;
784
785 sc = addr;
786 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
787 }
788
789 static int
790 fms_trigger_output(void *addr, void *start, void *end, int blksize,
791 void (*intr)(void *), void *arg, const audio_params_t *param)
792 {
793 struct fms_softc *sc;
794 struct fms_dma *p;
795
796 sc = addr;
797 sc->sc_pintr = intr;
798 sc->sc_parg = arg;
799
800 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
801 continue;
802
803 if (p == NULL)
804 panic("fms_trigger_output: request with bad start "
805 "address (%p)", start);
806
807 sc->sc_play_start = p->map->dm_segs[0].ds_addr;
808 sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
809 sc->sc_play_blksize = blksize;
810 sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
811 sc->sc_play_flip = 0;
812 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
813 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
814 sc->sc_play_start);
815 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
816 sc->sc_play_nextblk);
817 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
818 FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
819 return 0;
820 }
821
822
823 static int
824 fms_trigger_input(void *addr, void *start, void *end, int blksize,
825 void (*intr)(void *), void *arg, const audio_params_t *param)
826 {
827 struct fms_softc *sc;
828 struct fms_dma *p;
829
830 sc = addr;
831 sc->sc_rintr = intr;
832 sc->sc_rarg = arg;
833
834 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
835 continue;
836
837 if (p == NULL)
838 panic("fms_trigger_input: request with bad start "
839 "address (%p)", start);
840
841 sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
842 sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
843 sc->sc_rec_blksize = blksize;
844 sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
845 sc->sc_rec_flip = 0;
846 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
847 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
848 sc->sc_rec_start);
849 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
850 sc->sc_rec_nextblk);
851 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
852 FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
853 return 0;
854 }
855