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