autri.c revision 1.46.10.1 1 /* $NetBSD: autri.c,v 1.46.10.1 2012/04/17 00:07:43 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2001 SOMEYA Yoshihiko and KUROSAWA Takahiro.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Trident 4DWAVE-DX/NX, SiS 7018, ALi M5451 Sound Driver
30 *
31 * The register information is taken from the ALSA driver.
32 *
33 * Documentation links:
34 * - ftp://ftp.alsa-project.org/pub/manuals/trident/
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: autri.c,v 1.46.10.1 2012/04/17 00:07:43 yamt Exp $");
39
40 #include "midi.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/kmem.h>
47 #include <sys/device.h>
48 #include <sys/proc.h>
49 #include <sys/audioio.h>
50 #include <sys/bus.h>
51 #include <sys/intr.h>
52
53 #include <dev/audio_if.h>
54 #include <dev/midi_if.h>
55 #include <dev/mulaw.h>
56 #include <dev/auconv.h>
57
58 #include <dev/ic/ac97reg.h>
59 #include <dev/ic/ac97var.h>
60 #include <dev/ic/mpuvar.h>
61
62 #include <dev/pci/pcidevs.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcivar.h>
65 #include <dev/pci/autrireg.h>
66 #include <dev/pci/autrivar.h>
67
68 #ifdef AUDIO_DEBUG
69 # define DPRINTF(x) if (autridebug) printf x
70 # define DPRINTFN(n,x) if (autridebug > (n)) printf x
71 int autridebug = 0;
72 #else
73 # define DPRINTF(x)
74 # define DPRINTFN(n,x)
75 #endif
76
77 static int autri_intr(void *);
78
79 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
80 #define KERNADDR(p) ((void *)((p)->addr))
81
82 static int autri_allocmem(struct autri_softc *, size_t,
83 size_t, struct autri_dma *);
84 static int autri_freemem(struct autri_softc *, struct autri_dma *);
85
86 #define TWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
87 #define TWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
88 #define TWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
89 #define TREAD1(sc, r) bus_space_read_1((sc)->memt, (sc)->memh, (r))
90 #define TREAD2(sc, r) bus_space_read_2((sc)->memt, (sc)->memh, (r))
91 #define TREAD4(sc, r) bus_space_read_4((sc)->memt, (sc)->memh, (r))
92
93 static int autri_attach_codec(void *, struct ac97_codec_if *);
94 static int autri_read_codec(void *, uint8_t, uint16_t *);
95 static int autri_write_codec(void *, uint8_t, uint16_t);
96 static int autri_reset_codec(void *);
97 static enum ac97_host_flags autri_flags_codec(void *);
98
99 static bool autri_resume(device_t, const pmf_qual_t *);
100 static int autri_init(void *);
101 static struct autri_dma *autri_find_dma(struct autri_softc *, void *);
102 static void autri_setup_channel(struct autri_softc *, int,
103 const audio_params_t *param);
104 static void autri_enable_interrupt(struct autri_softc *, int);
105 static void autri_disable_interrupt(struct autri_softc *, int);
106 static void autri_startch(struct autri_softc *, int, int);
107 static void autri_stopch(struct autri_softc *, int, int);
108 static void autri_enable_loop_interrupt(void *);
109 #if 0
110 static void autri_disable_loop_interrupt(void *);
111 #endif
112
113 static int autri_open(void *, int);
114 static int autri_query_encoding(void *, struct audio_encoding *);
115 static int autri_set_params(void *, int, int, audio_params_t *,
116 audio_params_t *, stream_filter_list_t *,
117 stream_filter_list_t *);
118 static int autri_round_blocksize(void *, int, int, const audio_params_t *);
119 static int autri_trigger_output(void *, void *, void *, int,
120 void (*)(void *), void *,
121 const audio_params_t *);
122 static int autri_trigger_input(void *, void *, void *, int,
123 void (*)(void *), void *,
124 const audio_params_t *);
125 static int autri_halt_output(void *);
126 static int autri_halt_input(void *);
127 static int autri_getdev(void *, struct audio_device *);
128 static int autri_mixer_set_port(void *, mixer_ctrl_t *);
129 static int autri_mixer_get_port(void *, mixer_ctrl_t *);
130 static void* autri_malloc(void *, int, size_t);
131 static void autri_free(void *, void *, size_t);
132 static size_t autri_round_buffersize(void *, int, size_t);
133 static paddr_t autri_mappage(void *, void *, off_t, int);
134 static int autri_get_props(void *);
135 static int autri_query_devinfo(void *, mixer_devinfo_t *);
136 static void autri_get_locks(void *, kmutex_t **, kmutex_t **);
137
138 static const struct audio_hw_if autri_hw_if = {
139 autri_open,
140 NULL, /* close */
141 NULL, /* drain */
142 autri_query_encoding,
143 autri_set_params,
144 autri_round_blocksize,
145 NULL, /* commit_settings */
146 NULL, /* init_output */
147 NULL, /* init_input */
148 NULL, /* start_output */
149 NULL, /* start_input */
150 autri_halt_output,
151 autri_halt_input,
152 NULL, /* speaker_ctl */
153 autri_getdev,
154 NULL, /* setfd */
155 autri_mixer_set_port,
156 autri_mixer_get_port,
157 autri_query_devinfo,
158 autri_malloc,
159 autri_free,
160 autri_round_buffersize,
161 autri_mappage,
162 autri_get_props,
163 autri_trigger_output,
164 autri_trigger_input,
165 NULL, /* dev_ioctl */
166 autri_get_locks,
167 };
168
169 #if NMIDI > 0
170 static void autri_midi_close(void *);
171 static void autri_midi_getinfo(void *, struct midi_info *);
172 static int autri_midi_open(void *, int, void (*)(void *, int),
173 void (*)(void *), void *);
174 static int autri_midi_output(void *, int);
175
176 static const struct midi_hw_if autri_midi_hw_if = {
177 autri_midi_open,
178 autri_midi_close,
179 autri_midi_output,
180 autri_midi_getinfo,
181 NULL, /* ioctl */
182 autri_get_locks,
183 };
184 #endif
185
186 #define AUTRI_NFORMATS 8
187 static const struct audio_format autri_formats[AUTRI_NFORMATS] = {
188 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
189 2, AUFMT_STEREO, 0, {4000, 48000}},
190 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
191 1, AUFMT_MONAURAL, 0, {4000, 48000}},
192 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
193 2, AUFMT_STEREO, 0, {4000, 48000}},
194 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
195 1, AUFMT_MONAURAL, 0, {4000, 48000}},
196 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
197 2, AUFMT_STEREO, 0, {4000, 48000}},
198 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
199 1, AUFMT_MONAURAL, 0, {4000, 48000}},
200 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
201 2, AUFMT_STEREO, 0, {4000, 48000}},
202 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
203 1, AUFMT_MONAURAL, 0, {4000, 48000}},
204 };
205
206 /*
207 * register set/clear bit
208 */
209 static inline void
210 autri_reg_set_1(struct autri_softc *sc, int no, uint8_t mask)
211 {
212 bus_space_write_1(sc->memt, sc->memh, no,
213 (bus_space_read_1(sc->memt, sc->memh, no) | mask));
214 }
215
216 static inline void
217 autri_reg_clear_1(struct autri_softc *sc, int no, uint8_t mask)
218 {
219 bus_space_write_1(sc->memt, sc->memh, no,
220 (bus_space_read_1(sc->memt, sc->memh, no) & ~mask));
221 }
222
223 static inline void
224 autri_reg_set_4(struct autri_softc *sc, int no, uint32_t mask)
225 {
226 bus_space_write_4(sc->memt, sc->memh, no,
227 (bus_space_read_4(sc->memt, sc->memh, no) | mask));
228 }
229
230 static inline void
231 autri_reg_clear_4(struct autri_softc *sc, int no, uint32_t mask)
232 {
233 bus_space_write_4(sc->memt, sc->memh, no,
234 (bus_space_read_4(sc->memt, sc->memh, no) & ~mask));
235 }
236
237 /*
238 * AC'97 codec
239 */
240 static int
241 autri_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
242 {
243 struct autri_codec_softc *sc;
244
245 DPRINTF(("autri_attach_codec()\n"));
246 sc = sc_;
247 sc->codec_if = codec_if;
248 return 0;
249 }
250
251 static int
252 autri_read_codec(void *sc_, uint8_t index, uint16_t *data)
253 {
254 struct autri_codec_softc *codec;
255 struct autri_softc *sc;
256 uint32_t status, addr, cmd, busy;
257 uint16_t count;
258
259 codec = sc_;
260 sc = codec->sc;
261 /*DPRINTF(("sc->sc->type : 0x%X",sc->sc->type));*/
262
263 switch (sc->sc_devid) {
264 case AUTRI_DEVICE_ID_4DWAVE_DX:
265 addr = AUTRI_DX_ACR1;
266 cmd = AUTRI_DX_ACR1_CMD_READ;
267 busy = AUTRI_DX_ACR1_BUSY_READ;
268 break;
269 case AUTRI_DEVICE_ID_4DWAVE_NX:
270 addr = AUTRI_NX_ACR2;
271 cmd = AUTRI_NX_ACR2_CMD_READ;
272 busy = AUTRI_NX_ACR2_BUSY_READ | AUTRI_NX_ACR2_RECV_WAIT;
273 break;
274 case AUTRI_DEVICE_ID_SIS_7018:
275 addr = AUTRI_SIS_ACRD;
276 cmd = AUTRI_SIS_ACRD_CMD_READ;
277 busy = AUTRI_SIS_ACRD_BUSY_READ | AUTRI_SIS_ACRD_AUDIO_BUSY;
278 break;
279 case AUTRI_DEVICE_ID_ALI_M5451:
280 if (sc->sc_revision > 0x01)
281 addr = AUTRI_ALI_ACWR;
282 else
283 addr = AUTRI_ALI_ACRD;
284 cmd = AUTRI_ALI_ACRD_CMD_READ;
285 busy = AUTRI_ALI_ACRD_BUSY_READ;
286 break;
287 default:
288 printf("%s: autri_read_codec : unknown device\n",
289 device_xname(sc->sc_dev));
290 return -1;
291 }
292
293 /* wait for 'Ready to Read' */
294 for (count=0; count<0xffff; count++) {
295 if ((TREAD4(sc, addr) & busy) == 0)
296 break;
297 }
298
299 if (count == 0xffff) {
300 printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
301 device_xname(sc->sc_dev));
302 return -1;
303 }
304
305 /* send Read Command to AC'97 */
306 TWRITE4(sc, addr, (index & 0x7f) | cmd);
307
308 /* wait for 'Returned data is avalable' */
309 for (count=0; count<0xffff; count++) {
310 status = TREAD4(sc, addr);
311 if ((status & busy) == 0)
312 break;
313 }
314
315 if (count == 0xffff) {
316 printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
317 device_xname(sc->sc_dev));
318 return -1;
319 }
320
321 *data = (status >> 16) & 0x0000ffff;
322 /*DPRINTF(("autri_read_codec(0x%X) return 0x%X\n",reg,*data));*/
323 return 0;
324 }
325
326 static int
327 autri_write_codec(void *sc_, uint8_t index, uint16_t data)
328 {
329 struct autri_codec_softc *codec;
330 struct autri_softc *sc;
331 uint32_t addr, cmd, busy;
332 uint16_t count;
333
334 codec = sc_;
335 sc = codec->sc;
336 /*DPRINTF(("autri_write_codec(0x%X,0x%X)\n",index,data));*/
337
338 switch (sc->sc_devid) {
339 case AUTRI_DEVICE_ID_4DWAVE_DX:
340 addr = AUTRI_DX_ACR0;
341 cmd = AUTRI_DX_ACR0_CMD_WRITE;
342 busy = AUTRI_DX_ACR0_BUSY_WRITE;
343 break;
344 case AUTRI_DEVICE_ID_4DWAVE_NX:
345 addr = AUTRI_NX_ACR1;
346 cmd = AUTRI_NX_ACR1_CMD_WRITE;
347 busy = AUTRI_NX_ACR1_BUSY_WRITE;
348 break;
349 case AUTRI_DEVICE_ID_SIS_7018:
350 addr = AUTRI_SIS_ACWR;
351 cmd = AUTRI_SIS_ACWR_CMD_WRITE;
352 busy = AUTRI_SIS_ACWR_BUSY_WRITE | AUTRI_SIS_ACWR_AUDIO_BUSY;
353 break;
354 case AUTRI_DEVICE_ID_ALI_M5451:
355 addr = AUTRI_ALI_ACWR;
356 cmd = AUTRI_ALI_ACWR_CMD_WRITE;
357 if (sc->sc_revision > 0x01)
358 cmd |= 0x0100;
359 busy = AUTRI_ALI_ACWR_BUSY_WRITE;
360 break;
361 default:
362 printf("%s: autri_write_codec : unknown device.\n",
363 device_xname(sc->sc_dev));
364 return -1;
365 }
366
367 /* wait for 'Ready to Write' */
368 for (count=0; count<0xffff; count++) {
369 if ((TREAD4(sc, addr) & busy) == 0)
370 break;
371 }
372
373 if (count == 0xffff) {
374 printf("%s: Codec timeout. Busy writing AC'97 codec\n",
375 device_xname(sc->sc_dev));
376 return -1;
377 }
378
379 /* send Write Command to AC'97 */
380 TWRITE4(sc, addr, (data << 16) | (index & 0x7f) | cmd);
381
382 return 0;
383 }
384
385 static int
386 autri_reset_codec(void *sc_)
387 {
388 struct autri_codec_softc *codec;
389 struct autri_softc *sc;
390 uint32_t reg, ready;
391 int addr, count;
392
393 codec = sc_;
394 sc = codec->sc;
395 count = 200;
396 DPRINTF(("autri_reset_codec(codec=%p,sc=%p)\n", codec, sc));
397 DPRINTF(("sc->sc_devid=%X\n", sc->sc_devid));
398
399 switch (sc->sc_devid) {
400 case AUTRI_DEVICE_ID_4DWAVE_DX:
401 /* warm reset AC'97 codec */
402 autri_reg_set_4(sc, AUTRI_DX_ACR2, 1);
403 delay(100);
404 /* release reset */
405 autri_reg_clear_4(sc, AUTRI_DX_ACR2, 1);
406 delay(100);
407
408 addr = AUTRI_DX_ACR2;
409 ready = AUTRI_DX_ACR2_CODEC_READY;
410 break;
411 case AUTRI_DEVICE_ID_4DWAVE_NX:
412 /* warm reset AC'97 codec */
413 autri_reg_set_4(sc, AUTRI_NX_ACR0, 1);
414 delay(100);
415 /* release reset */
416 autri_reg_clear_4(sc, AUTRI_NX_ACR0, 1);
417 delay(100);
418
419 addr = AUTRI_NX_ACR0;
420 ready = AUTRI_NX_ACR0_CODEC_READY;
421 break;
422 case AUTRI_DEVICE_ID_SIS_7018:
423 /* cold reset AC'97 codec */
424 autri_reg_set_4(sc, AUTRI_SIS_SCTRL, 2);
425 delay(1000);
426 /* release reset (warm & cold) */
427 autri_reg_clear_4(sc, AUTRI_SIS_SCTRL, 3);
428 delay(2000);
429
430 addr = AUTRI_SIS_SCTRL;
431 ready = AUTRI_SIS_SCTRL_CODEC_READY;
432 break;
433 case AUTRI_DEVICE_ID_ALI_M5451:
434 /* warm reset AC'97 codec */
435 autri_reg_set_4(sc, AUTRI_ALI_SCTRL, 1);
436 delay(100);
437 /* release reset (warm & cold) */
438 autri_reg_clear_4(sc, AUTRI_ALI_SCTRL, 3);
439 delay(100);
440
441 addr = AUTRI_ALI_SCTRL;
442 ready = AUTRI_ALI_SCTRL_CODEC_READY;
443 break;
444 default:
445 printf("%s: autri_reset_codec : unknown device\n",
446 device_xname(sc->sc_dev));
447 return EOPNOTSUPP;
448 }
449
450 /* wait for 'Codec Ready' */
451 while (count--) {
452 reg = TREAD4(sc, addr);
453 if (reg & ready)
454 break;
455 delay(1000);
456 }
457
458 if (count == 0) {
459 printf("%s: Codec timeout. AC'97 is not ready for operation.\n",
460 device_xname(sc->sc_dev));
461 return ETIMEDOUT;
462 }
463 return 0;
464 }
465
466 static enum ac97_host_flags
467 autri_flags_codec(void *sc)
468 {
469 return AC97_HOST_DONT_READ;
470 }
471
472 /*
473 *
474 */
475
476 static int
477 autri_match(device_t parent, cfdata_t match, void *aux)
478 {
479 struct pci_attach_args *pa;
480
481 pa = (struct pci_attach_args *)aux;
482 switch (PCI_VENDOR(pa->pa_id)) {
483 case PCI_VENDOR_TRIDENT:
484 switch (PCI_PRODUCT(pa->pa_id)) {
485 case PCI_PRODUCT_TRIDENT_4DWAVE_DX:
486 /*
487 * IBM makes a pcn network card and improperly
488 * sets the vendor and product ID's. Avoid matching.
489 */
490 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK)
491 return 0;
492 /* FALLTHROUGH */
493 case PCI_PRODUCT_TRIDENT_4DWAVE_NX:
494 return 1;
495 }
496 break;
497 case PCI_VENDOR_SIS:
498 switch (PCI_PRODUCT(pa->pa_id)) {
499 case PCI_PRODUCT_SIS_7018:
500 return 1;
501 }
502 break;
503 case PCI_VENDOR_ALI:
504 switch (PCI_PRODUCT(pa->pa_id)) {
505 case PCI_PRODUCT_ALI_M5451:
506 return 1;
507 }
508 break;
509 }
510
511 return 0;
512 }
513
514 static void
515 autri_attach(device_t parent, device_t self, void *aux)
516 {
517 struct autri_softc *sc;
518 struct pci_attach_args *pa;
519 pci_chipset_tag_t pc;
520 struct autri_codec_softc *codec;
521 pci_intr_handle_t ih;
522 char const *intrstr;
523 int r;
524 uint32_t reg;
525
526 sc = device_private(self);
527 sc->sc_dev = self;
528 pa = (struct pci_attach_args *)aux;
529 pc = pa->pa_pc;
530
531 sc->sc_devid = pa->pa_id;
532 sc->sc_class = pa->pa_class;
533
534 pci_aprint_devinfo(pa, "Audio controller");
535 sc->sc_revision = PCI_REVISION(pa->pa_class);
536
537 /* map register to memory */
538 if (pci_mapreg_map(pa, AUTRI_PCI_MEMORY_BASE,
539 PCI_MAPREG_TYPE_MEM, 0, &sc->memt, &sc->memh, NULL, NULL)) {
540 aprint_error_dev(sc->sc_dev, "can't map memory space\n");
541 return;
542 }
543
544 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
545 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
546
547 /* map and establish the interrupt */
548 if (pci_intr_map(pa, &ih)) {
549 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
550 return;
551 }
552 intrstr = pci_intr_string(pc, ih);
553 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, autri_intr, sc);
554 if (sc->sc_ih == NULL) {
555 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
556 if (intrstr != NULL)
557 aprint_error(" at %s", intrstr);
558 aprint_error("\n");
559 return;
560 }
561 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
562
563 sc->sc_dmatag = pa->pa_dmat;
564 sc->sc_pc = pc;
565 sc->sc_pt = pa->pa_tag;
566
567 /* enable the device */
568 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
569 reg |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
570 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
571
572 /* initialize the device */
573 autri_init(sc);
574
575 /* attach AC'97 codec */
576 codec = &sc->sc_codec;
577 codec->sc = sc;
578
579 codec->host_if.arg = codec;
580 codec->host_if.attach = autri_attach_codec;
581 codec->host_if.reset = autri_reset_codec;
582 codec->host_if.read = autri_read_codec;
583 codec->host_if.write = autri_write_codec;
584 codec->host_if.flags = autri_flags_codec;
585
586 r = ac97_attach(&codec->host_if, self, &sc->sc_lock);
587 if (r != 0) {
588 aprint_error_dev(sc->sc_dev, "can't attach codec (error 0x%X)\n", r);
589 return;
590 }
591
592 if (!pmf_device_register(self, NULL, autri_resume))
593 aprint_error_dev(self, "couldn't establish power handler\n");
594
595 audio_attach_mi(&autri_hw_if, sc, sc->sc_dev);
596
597 #if NMIDI > 0
598 midi_attach_mi(&autri_midi_hw_if, sc, sc->sc_dev);
599 #endif
600 }
601
602 CFATTACH_DECL_NEW(autri, sizeof(struct autri_softc),
603 autri_match, autri_attach, NULL, NULL);
604
605 static bool
606 autri_resume(device_t dv, const pmf_qual_t *qual)
607 {
608 struct autri_softc *sc = device_private(dv);
609
610 mutex_enter(&sc->sc_lock);
611 mutex_spin_enter(&sc->sc_intr_lock);
612 autri_init(sc);
613 mutex_spin_exit(&sc->sc_intr_lock);
614 (sc->sc_codec.codec_if->vtbl->restore_ports)(sc->sc_codec.codec_if);
615 mutex_exit(&sc->sc_lock);
616
617 return true;
618 }
619
620 static int
621 autri_init(void *sc_)
622 {
623 struct autri_softc *sc;
624 uint32_t reg;
625 pci_chipset_tag_t pc;
626 pcitag_t pt;
627
628 sc = sc_;
629 pc = sc->sc_pc;
630 pt = sc->sc_pt;
631 DPRINTF(("in autri_init()\n"));
632 DPRINTFN(5,("pci_conf_read(0x40) : 0x%X\n",pci_conf_read(pc,pt,0x40)));
633 DPRINTFN(5,("pci_conf_read(0x44) : 0x%X\n",pci_conf_read(pc,pt,0x44)));
634
635 switch (sc->sc_devid) {
636 case AUTRI_DEVICE_ID_4DWAVE_DX:
637 /* disable Legacy Control */
638 pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
639 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
640 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
641 delay(100);
642 /* audio engine reset */
643 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
644 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00040000);
645 delay(100);
646 /* release reset */
647 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
648 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
649 delay(100);
650 /* DAC on */
651 autri_reg_set_4(sc,AUTRI_DX_ACR2,0x02);
652 break;
653 case AUTRI_DEVICE_ID_4DWAVE_NX:
654 /* disable Legacy Control */
655 pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
656 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
657 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
658 delay(100);
659 /* audio engine reset */
660 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
661 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00010000);
662 delay(100);
663 /* release reset */
664 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
665 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00010000);
666 delay(100);
667 /* DAC on */
668 autri_reg_set_4(sc,AUTRI_NX_ACR0,0x02);
669 break;
670 case AUTRI_DEVICE_ID_SIS_7018:
671 /* disable Legacy Control */
672 pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
673 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
674 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
675 delay(100);
676 /* reset Digital Controller */
677 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
678 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
679 delay(100);
680 /* release reset */
681 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
682 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
683 delay(100);
684 /* disable AC97 GPIO interrupt */
685 TWRITE1(sc, AUTRI_SIS_ACGPIO, 0);
686 /* enable 64 channel mode */
687 autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, BANK_B_EN);
688 break;
689 case AUTRI_DEVICE_ID_ALI_M5451:
690 /* disable Legacy Control */
691 pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
692 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
693 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
694 delay(100);
695 /* reset Digital Controller */
696 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
697 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
698 delay(100);
699 /* release reset */
700 reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
701 pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
702 delay(100);
703 /* enable PCM input */
704 autri_reg_set_4(sc, AUTRI_ALI_GCONTROL, AUTRI_ALI_GCONTROL_PCM_IN);
705 break;
706 }
707
708 if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
709 sc->sc_play.ch = 0;
710 sc->sc_play.ch_intr = 1;
711 sc->sc_rec.ch = 31;
712 sc->sc_rec.ch_intr = 2;
713 } else {
714 sc->sc_play.ch = 0x20;
715 sc->sc_play.ch_intr = 0x21;
716 sc->sc_rec.ch = 0x22;
717 sc->sc_rec.ch_intr = 0x23;
718 }
719
720 /* clear channel status */
721 TWRITE4(sc, AUTRI_STOP_A, 0xffffffff);
722 TWRITE4(sc, AUTRI_STOP_B, 0xffffffff);
723
724 /* disable channel interrupt */
725 TWRITE4(sc, AUTRI_AINTEN_A, 0);
726 TWRITE4(sc, AUTRI_AINTEN_B, 0);
727
728 #if 0
729 /* TLB */
730 if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
731 TWRITE4(sc,AUTRI_NX_TLBC,0);
732 }
733 #endif
734
735 autri_enable_loop_interrupt(sc);
736
737 DPRINTF(("out autri_init()\n"));
738 return 0;
739 }
740
741 static void
742 autri_enable_loop_interrupt(void *sc_)
743 {
744 struct autri_softc *sc;
745 uint32_t reg;
746
747 /*reg = (ENDLP_IE | MIDLP_IE);*/
748 reg = ENDLP_IE;
749 sc = sc_;
750 if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
751 reg |= BANK_B_EN;
752
753 autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, reg);
754 }
755
756 #if 0
757 static void
758 autri_disable_loop_interrupt(void *sc_)
759 {
760 struct autri_softc *sc;
761 uint32_t reg;
762
763 reg = (ENDLP_IE | MIDLP_IE);
764 sc = sc_;
765 autri_reg_clear_4(sc, AUTRI_LFO_GC_CIR, reg);
766 }
767 #endif
768
769 static int
770 autri_intr(void *p)
771 {
772 struct autri_softc *sc;
773 uint32_t intsrc;
774 uint32_t mask, active[2];
775 int ch, endch;
776 /*
777 u_int32_t reg;
778 u_int32_t cso,eso;
779 */
780 sc = p;
781 mutex_spin_enter(&sc->sc_intr_lock);
782
783 intsrc = TREAD4(sc, AUTRI_MISCINT);
784 if ((intsrc & (ADDRESS_IRQ | MPU401_IRQ)) == 0) {
785 mutex_spin_exit(&sc->sc_intr_lock);
786 return 0;
787 }
788
789 if (intsrc & ADDRESS_IRQ) {
790
791 active[0] = TREAD4(sc,AUTRI_AIN_A);
792 active[1] = TREAD4(sc,AUTRI_AIN_B);
793
794 if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
795 endch = 32;
796 } else {
797 endch = 64;
798 }
799
800 for (ch = 0; ch < endch; ch++) {
801 mask = 1 << (ch & 0x1f);
802 if (active[(ch & 0x20) ? 1 : 0] & mask) {
803
804 /* clear interrupt */
805 TWRITE4(sc, (ch & 0x20) ? AUTRI_AIN_B : AUTRI_AIN_A, mask);
806 /* disable interrupt */
807 autri_reg_clear_4(sc,(ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
808 #if 0
809 reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
810 TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | ch);
811
812 if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
813 cso = TREAD4(sc, 0xe0) & 0x00ffffff;
814 eso = TREAD4(sc, 0xe8) & 0x00ffffff;
815 } else {
816 cso = (TREAD4(sc, 0xe0) >> 16) & 0x0000ffff;
817 eso = (TREAD4(sc, 0xe8) >> 16) & 0x0000ffff;
818 }
819 /*printf("cso=%d, eso=%d\n",cso,eso);*/
820 #endif
821 if (ch == sc->sc_play.ch_intr) {
822 if (sc->sc_play.intr)
823 sc->sc_play.intr(sc->sc_play.intr_arg);
824 }
825
826 if (ch == sc->sc_rec.ch_intr) {
827 if (sc->sc_rec.intr)
828 sc->sc_rec.intr(sc->sc_rec.intr_arg);
829 }
830
831 /* enable interrupt */
832 autri_reg_set_4(sc, (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
833 }
834 }
835 }
836
837 if (intsrc & MPU401_IRQ) {
838 /* XXX */
839 }
840
841 autri_reg_set_4(sc,AUTRI_MISCINT,
842 ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW);
843
844 mutex_spin_exit(&sc->sc_intr_lock);
845 return 1;
846 }
847
848 /*
849 *
850 */
851
852 static int
853 autri_allocmem(struct autri_softc *sc, size_t size, size_t align,
854 struct autri_dma *p)
855 {
856 int error;
857
858 p->size = size;
859 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
860 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
861 &p->nsegs, BUS_DMA_WAITOK);
862 if (error)
863 return error;
864
865 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
866 &p->addr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
867 if (error)
868 goto free;
869
870 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
871 0, BUS_DMA_WAITOK, &p->map);
872 if (error)
873 goto unmap;
874
875 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
876 BUS_DMA_WAITOK);
877 if (error)
878 goto destroy;
879 return (0);
880
881 destroy:
882 bus_dmamap_destroy(sc->sc_dmatag, p->map);
883 unmap:
884 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
885 free:
886 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
887 return error;
888 }
889
890 static int
891 autri_freemem(struct autri_softc *sc, struct autri_dma *p)
892 {
893
894 bus_dmamap_unload(sc->sc_dmatag, p->map);
895 bus_dmamap_destroy(sc->sc_dmatag, p->map);
896 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
897 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
898 return 0;
899 }
900
901 static int
902 autri_open(void *addr, int flags)
903 {
904 DPRINTF(("autri_open()\n"));
905 DPRINTFN(5,("MISCINT : 0x%08X\n",
906 TREAD4((struct autri_softc *)addr, AUTRI_MISCINT)));
907 DPRINTFN(5,("LFO_GC_CIR : 0x%08X\n",
908 TREAD4((struct autri_softc *)addr, AUTRI_LFO_GC_CIR)));
909 return 0;
910 }
911
912 static int
913 autri_query_encoding(void *addr, struct audio_encoding *fp)
914 {
915
916 switch (fp->index) {
917 case 0:
918 strcpy(fp->name, AudioEulinear);
919 fp->encoding = AUDIO_ENCODING_ULINEAR;
920 fp->precision = 8;
921 fp->flags = 0;
922 break;
923 case 1:
924 strcpy(fp->name, AudioEmulaw);
925 fp->encoding = AUDIO_ENCODING_ULAW;
926 fp->precision = 8;
927 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
928 break;
929 case 2:
930 strcpy(fp->name, AudioEalaw);
931 fp->encoding = AUDIO_ENCODING_ALAW;
932 fp->precision = 8;
933 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
934 break;
935 case 3:
936 strcpy(fp->name, AudioEslinear);
937 fp->encoding = AUDIO_ENCODING_SLINEAR;
938 fp->precision = 8;
939 fp->flags = 0;
940 break;
941 case 4:
942 strcpy(fp->name, AudioEslinear_le);
943 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
944 fp->precision = 16;
945 fp->flags = 0;
946 break;
947 case 5:
948 strcpy(fp->name, AudioEulinear_le);
949 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
950 fp->precision = 16;
951 fp->flags = 0;
952 break;
953 case 6:
954 strcpy(fp->name, AudioEslinear_be);
955 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
956 fp->precision = 16;
957 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
958 break;
959 case 7:
960 strcpy(fp->name, AudioEulinear_be);
961 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
962 fp->precision = 16;
963 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
964 break;
965 default:
966 return EINVAL;
967 }
968
969 return 0;
970 }
971
972 static int
973 autri_set_params(void *addr, int setmode, int usemode,
974 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
975 stream_filter_list_t *rfil)
976 {
977 if (setmode & AUMODE_RECORD) {
978 if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
979 AUMODE_RECORD, rec, FALSE, rfil) < 0)
980 return EINVAL;
981 }
982 if (setmode & AUMODE_PLAY) {
983 if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
984 AUMODE_PLAY, play, FALSE, pfil) < 0)
985 return EINVAL;
986 }
987 return 0;
988 }
989
990 static int
991 autri_round_blocksize(void *addr, int block,
992 int mode, const audio_params_t *param)
993 {
994 return block & -4;
995 }
996
997 static int
998 autri_halt_output(void *addr)
999 {
1000 struct autri_softc *sc;
1001
1002 DPRINTF(("autri_halt_output()\n"));
1003 sc = addr;
1004 sc->sc_play.intr = NULL;
1005 autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1006 autri_disable_interrupt(sc, sc->sc_play.ch_intr);
1007
1008 return 0;
1009 }
1010
1011 static int
1012 autri_halt_input(void *addr)
1013 {
1014 struct autri_softc *sc;
1015
1016 DPRINTF(("autri_halt_input()\n"));
1017 sc = addr;
1018 sc->sc_rec.intr = NULL;
1019 autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1020 autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
1021
1022 return 0;
1023 }
1024
1025 static int
1026 autri_getdev(void *addr, struct audio_device *retp)
1027 {
1028 struct autri_softc *sc;
1029
1030 DPRINTF(("autri_getdev().\n"));
1031 sc = addr;
1032 strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
1033 snprintf(retp->version, sizeof(retp->version), "0x%02x",
1034 PCI_REVISION(sc->sc_class));
1035
1036 switch (sc->sc_devid) {
1037 case AUTRI_DEVICE_ID_4DWAVE_DX:
1038 strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
1039 break;
1040 case AUTRI_DEVICE_ID_4DWAVE_NX:
1041 strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
1042 break;
1043 case AUTRI_DEVICE_ID_SIS_7018:
1044 strncpy(retp->config, "SiS 7018", sizeof(retp->config));
1045 break;
1046 case AUTRI_DEVICE_ID_ALI_M5451:
1047 strncpy(retp->config, "ALi M5451", sizeof(retp->config));
1048 break;
1049 default:
1050 strncpy(retp->config, "unknown", sizeof(retp->config));
1051 }
1052
1053 return 0;
1054 }
1055
1056 static int
1057 autri_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1058 {
1059 struct autri_softc *sc;
1060
1061 sc = addr;
1062 return sc->sc_codec.codec_if->vtbl->mixer_set_port(
1063 sc->sc_codec.codec_if, cp);
1064 }
1065
1066 static int
1067 autri_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1068 {
1069 struct autri_softc *sc;
1070
1071 sc = addr;
1072 return sc->sc_codec.codec_if->vtbl->mixer_get_port(
1073 sc->sc_codec.codec_if, cp);
1074 }
1075
1076 static int
1077 autri_query_devinfo(void *addr, mixer_devinfo_t *dip)
1078 {
1079 struct autri_softc *sc;
1080
1081 sc = addr;
1082 return sc->sc_codec.codec_if->vtbl->query_devinfo(
1083 sc->sc_codec.codec_if, dip);
1084 }
1085
1086 static void *
1087 autri_malloc(void *addr, int direction, size_t size)
1088 {
1089 struct autri_softc *sc;
1090 struct autri_dma *p;
1091 int error;
1092
1093 p = kmem_alloc(sizeof(*p), KM_SLEEP);
1094 if (!p)
1095 return NULL;
1096 sc = addr;
1097 #if 0
1098 error = autri_allocmem(sc, size, 16, p);
1099 #endif
1100 error = autri_allocmem(sc, size, 0x10000, p);
1101 if (error) {
1102 kmem_free(p, sizeof(*p));
1103 return NULL;
1104 }
1105
1106 p->next = sc->sc_dmas;
1107 sc->sc_dmas = p;
1108 return KERNADDR(p);
1109 }
1110
1111 static void
1112 autri_free(void *addr, void *ptr, size_t size)
1113 {
1114 struct autri_softc *sc;
1115 struct autri_dma **pp, *p;
1116
1117 sc = addr;
1118 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1119 if (KERNADDR(p) == ptr) {
1120 autri_freemem(sc, p);
1121 *pp = p->next;
1122 kmem_free(p, sizeof(*p));
1123 return;
1124 }
1125 }
1126 }
1127
1128 static struct autri_dma *
1129 autri_find_dma(struct autri_softc *sc, void *addr)
1130 {
1131 struct autri_dma *p;
1132
1133 for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1134 continue;
1135
1136 return p;
1137 }
1138
1139 static size_t
1140 autri_round_buffersize(void *addr, int direction, size_t size)
1141 {
1142
1143 return size;
1144 }
1145
1146 static paddr_t
1147 autri_mappage(void *addr, void *mem, off_t off, int prot)
1148 {
1149 struct autri_softc *sc;
1150 struct autri_dma *p;
1151
1152 if (off < 0)
1153 return -1;
1154 sc = addr;
1155 p = autri_find_dma(sc, mem);
1156 if (!p)
1157 return -1;
1158
1159 return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1160 off, prot, BUS_DMA_WAITOK);
1161 }
1162
1163 static int
1164 autri_get_props(void *addr)
1165 {
1166 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1167 AUDIO_PROP_FULLDUPLEX;
1168 }
1169
1170 static void
1171 autri_setup_channel(struct autri_softc *sc, int mode,
1172 const audio_params_t *param)
1173 {
1174 int i, ch, channel;
1175 uint32_t reg, cr[5];
1176 uint32_t cso, eso;
1177 uint32_t delta, dch[2], ctrl;
1178 uint32_t alpha_fms, fm_vol, attribute;
1179
1180 uint32_t dmaaddr, dmalen;
1181 int factor, rvol, cvol;
1182 struct autri_chstatus *chst;
1183
1184 ctrl = AUTRI_CTRL_LOOPMODE;
1185 switch (param->encoding) {
1186 case AUDIO_ENCODING_SLINEAR_BE:
1187 case AUDIO_ENCODING_SLINEAR_LE:
1188 ctrl |= AUTRI_CTRL_SIGNED;
1189 break;
1190 }
1191
1192 factor = 0;
1193 if (param->precision == 16) {
1194 ctrl |= AUTRI_CTRL_16BIT;
1195 factor++;
1196 }
1197
1198 if (param->channels == 2) {
1199 ctrl |= AUTRI_CTRL_STEREO;
1200 factor++;
1201 }
1202
1203 delta = param->sample_rate;
1204 if (delta < 4000)
1205 delta = 4000;
1206 if (delta > 48000)
1207 delta = 48000;
1208
1209 attribute = 0;
1210
1211 dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
1212 if (mode == AUMODE_PLAY) {
1213 chst = &sc->sc_play;
1214 dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
1215 ctrl |= AUTRI_CTRL_WAVEVOL;
1216 } else {
1217 chst = &sc->sc_rec;
1218 dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
1219 if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018) {
1220 ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
1221 attribute = AUTRI_ATTR_PCMREC_SIS;
1222 if (delta != 48000)
1223 attribute |= AUTRI_ATTR_ENASRC_SIS;
1224 } else
1225 ctrl |= AUTRI_CTRL_MUTEVOL;
1226 }
1227
1228 dmaaddr = DMAADDR(chst->dma);
1229 cso = alpha_fms = 0;
1230 rvol = cvol = 0x7f;
1231 fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
1232
1233 for (ch = 0; ch < 2; ch++) {
1234
1235 if (ch == 0)
1236 dmalen = (chst->length >> factor);
1237 else {
1238 /* channel for interrupt */
1239 dmalen = (chst->blksize >> factor);
1240 if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
1241 ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
1242 else
1243 ctrl |= AUTRI_CTRL_MUTEVOL;
1244 attribute = 0;
1245 cso = dmalen - 1;
1246 }
1247
1248 eso = dmalen - 1;
1249
1250 switch (sc->sc_devid) {
1251 case AUTRI_DEVICE_ID_4DWAVE_DX:
1252 cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1253 cr[1] = dmaaddr;
1254 cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1255 cr[3] = fm_vol;
1256 cr[4] = ctrl;
1257 break;
1258 case AUTRI_DEVICE_ID_4DWAVE_NX:
1259 cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
1260 cr[1] = dmaaddr;
1261 cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
1262 cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
1263 cr[4] = ctrl;
1264 break;
1265 case AUTRI_DEVICE_ID_SIS_7018:
1266 cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1267 cr[1] = dmaaddr;
1268 cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1269 cr[3] = attribute;
1270 cr[4] = ctrl;
1271 break;
1272 case AUTRI_DEVICE_ID_ALI_M5451:
1273 cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1274 cr[1] = dmaaddr;
1275 cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1276 cr[3] = 0;
1277 cr[4] = ctrl;
1278 break;
1279 }
1280
1281 /* write channel data */
1282 channel = (ch == 0) ? chst->ch : chst->ch_intr;
1283
1284 reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
1285 TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
1286
1287 for (i = 0; i < 5; i++) {
1288 TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
1289 DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
1290 }
1291
1292 /* Bank A only */
1293 if (channel < 0x20) {
1294 TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
1295 TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
1296 }
1297 }
1298
1299 }
1300
1301 static int
1302 autri_trigger_output(void *addr, void *start, void *end, int blksize,
1303 void (*intr)(void *), void *arg,
1304 const audio_params_t *param)
1305 {
1306 struct autri_softc *sc;
1307 struct autri_dma *p;
1308
1309 DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
1310 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1311 sc = addr;
1312 sc->sc_play.intr = intr;
1313 sc->sc_play.intr_arg = arg;
1314 sc->sc_play.offset = 0;
1315 sc->sc_play.blksize = blksize;
1316 sc->sc_play.length = (char *)end - (char *)start;
1317
1318 p = autri_find_dma(sc, start);
1319 if (!p) {
1320 printf("autri_trigger_output: bad addr %p\n", start);
1321 return (EINVAL);
1322 }
1323
1324 sc->sc_play.dma = p;
1325
1326 /* */
1327 autri_setup_channel(sc, AUMODE_PLAY, param);
1328
1329 /* volume set to no attenuation */
1330 TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
1331
1332 /* enable interrupt */
1333 autri_enable_interrupt(sc, sc->sc_play.ch_intr);
1334
1335 /* start channel */
1336 autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1337
1338 return 0;
1339 }
1340
1341 static int
1342 autri_trigger_input(void *addr, void *start, void *end, int blksize,
1343 void (*intr)(void *), void *arg,
1344 const audio_params_t *param)
1345 {
1346 struct autri_softc *sc;
1347 struct autri_dma *p;
1348
1349 DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
1350 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1351 sc = addr;
1352 sc->sc_rec.intr = intr;
1353 sc->sc_rec.intr_arg = arg;
1354 sc->sc_rec.offset = 0;
1355 sc->sc_rec.blksize = blksize;
1356 sc->sc_rec.length = (char *)end - (char *)start;
1357
1358 /* */
1359 p = autri_find_dma(sc, start);
1360 if (!p) {
1361 printf("autri_trigger_input: bad addr %p\n", start);
1362 return (EINVAL);
1363 }
1364
1365 sc->sc_rec.dma = p;
1366
1367 /* */
1368 if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
1369 autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
1370 TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
1371 }
1372
1373 #if 0
1374 /* 4DWAVE only allows capturing at a 48 kHz rate */
1375 if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
1376 sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
1377 param->sample_rate = 48000;
1378 #endif
1379
1380 autri_setup_channel(sc, AUMODE_RECORD, param);
1381
1382 /* enable interrupt */
1383 autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
1384
1385 /* start channel */
1386 autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1387
1388 return 0;
1389 }
1390
1391
1392 static void
1393 autri_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
1394 {
1395 struct autri_softc *sc;
1396
1397 sc = addr;
1398 *intr = &sc->sc_intr_lock;
1399 *proc = &sc->sc_lock;
1400 }
1401
1402 #if 0
1403 static int
1404 autri_halt(struct autri_softc *sc)
1405 {
1406
1407 DPRINTF(("autri_halt().\n"));
1408 /*autri_stopch(sc);*/
1409 autri_disable_interrupt(sc, sc->sc_play.channel);
1410 autri_disable_interrupt(sc, sc->sc_rec.channel);
1411 return 0;
1412 }
1413 #endif
1414
1415 static void
1416 autri_enable_interrupt(struct autri_softc *sc, int ch)
1417 {
1418 int reg;
1419
1420 reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1421 ch &= 0x1f;
1422
1423 autri_reg_set_4(sc, reg, 1 << ch);
1424 }
1425
1426 static void
1427 autri_disable_interrupt(struct autri_softc *sc, int ch)
1428 {
1429 int reg;
1430
1431 reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1432 ch &= 0x1f;
1433
1434 autri_reg_clear_4(sc, reg, 1 << ch);
1435 }
1436
1437 static void
1438 autri_startch(struct autri_softc *sc, int ch, int ch_intr)
1439 {
1440 int reg;
1441 uint32_t chmask;
1442
1443 reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
1444 ch &= 0x1f;
1445 ch_intr &= 0x1f;
1446 chmask = (1 << ch) | (1 << ch_intr);
1447
1448 autri_reg_set_4(sc, reg, chmask);
1449 }
1450
1451 static void
1452 autri_stopch(struct autri_softc *sc, int ch, int ch_intr)
1453 {
1454 int reg;
1455 uint32_t chmask;
1456
1457 reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
1458 ch &= 0x1f;
1459 ch_intr &= 0x1f;
1460 chmask = (1 << ch) | (1 << ch_intr);
1461
1462 autri_reg_set_4(sc, reg, chmask);
1463 }
1464
1465 #if NMIDI > 0
1466 static int
1467 autri_midi_open(void *addr, int flags, void (*iintr)(void *, int),
1468 void (*ointr)(void *), void *arg)
1469 {
1470 struct autri_softc *sc;
1471
1472 DPRINTF(("autri_midi_open()\n"));
1473 sc = addr;
1474 DPRINTFN(5,("MPUR1 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR1)));
1475 DPRINTFN(5,("MPUR2 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR2)));
1476
1477 sc->sc_iintr = iintr;
1478 sc->sc_ointr = ointr;
1479 sc->sc_arg = arg;
1480
1481 if (flags & FREAD)
1482 autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
1483
1484 if (flags & FWRITE)
1485 autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
1486
1487 return 0;
1488 }
1489
1490 static void
1491 autri_midi_close(void *addr)
1492 {
1493 struct autri_softc *sc;
1494
1495 DPRINTF(("autri_midi_close()\n"));
1496 sc = addr;
1497 kpause("autri", FALSE, hz/10, &sc->sc_lock); /* give uart a chance to drain */
1498
1499 sc->sc_iintr = NULL;
1500 sc->sc_ointr = NULL;
1501 }
1502
1503 static int
1504 autri_midi_output(void *addr, int d)
1505 {
1506 struct autri_softc *sc;
1507 int x;
1508
1509 sc = addr;
1510 for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1511 if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
1512 TWRITE1(sc, AUTRI_MPUR0, d);
1513 return 0;
1514 }
1515 delay(MIDI_BUSY_DELAY);
1516 }
1517 return EIO;
1518 }
1519
1520 static void
1521 autri_midi_getinfo(void *addr, struct midi_info *mi)
1522 {
1523
1524 mi->name = "4DWAVE MIDI UART";
1525 mi->props = MIDI_PROP_CAN_INPUT;
1526 }
1527
1528 #endif
1529