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