1 /* $OpenBSD: auacer.c,v 1.30 2024/05/24 06:02:53 jsg Exp $ */
2 /* $NetBSD: auacer.c,v 1.3 2004/11/10 04:20:26 kent Exp $ */
3
4 /*-
5 * Copyright (c) 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Acer Labs M5455 audio driver
35 *
36 * Acer provides data sheets after signing an NDA.
37 * The chip behaves somewhat like the Intel i8x0, so this driver
38 * is loosely based on the auich driver. Additional information taken from
39 * the ALSA intel8x0.c driver (which handles M5455 as well).
40 */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46
47 #include <dev/pci/pcidevs.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/auacerreg.h>
50
51 #include <sys/audioio.h>
52 #include <dev/audio_if.h>
53
54 #include <machine/bus.h>
55
56 #include <dev/ic/ac97.h>
57
58 struct auacer_dma {
59 bus_dmamap_t map;
60 caddr_t addr;
61 bus_dma_segment_t segs[1];
62 int nsegs;
63 size_t size;
64 struct auacer_dma *next;
65 };
66
67 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
68 #define KERNADDR(p) ((void *)((p)->addr))
69
70 const struct pci_matchid auacer_pci_devices[] = {
71 { PCI_VENDOR_ALI, PCI_PRODUCT_ALI_M5455 }
72 };
73
74 struct auacer_cdata {
75 struct auacer_dmalist ic_dmalist_pcmo[ALI_DMALIST_MAX];
76 };
77
78 struct auacer_chan {
79 uint32_t ptr;
80 uint32_t start, p, end;
81 uint32_t blksize, fifoe;
82 uint32_t ack;
83 uint32_t port;
84 struct auacer_dmalist *dmalist;
85 void (*intr)(void *);
86 void *arg;
87 };
88
89 struct auacer_softc {
90 struct device sc_dev;
91 void *sc_ih;
92
93 bus_space_tag_t iot;
94 bus_space_handle_t mix_ioh;
95 bus_space_handle_t aud_ioh;
96 bus_dma_tag_t dmat;
97
98 struct ac97_codec_if *codec_if;
99 struct ac97_host_if host_if;
100
101 /* DMA scatter-gather lists. */
102 bus_dmamap_t sc_cddmamap;
103 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
104
105 struct auacer_cdata *sc_cdata;
106
107 struct auacer_chan sc_pcmo;
108
109 struct auacer_dma *sc_dmas;
110
111 pci_chipset_tag_t sc_pc;
112 pcitag_t sc_pt;
113
114 int sc_dmamap_flags;
115 };
116
117 #define READ1(sc, a) bus_space_read_1(sc->iot, sc->aud_ioh, a)
118 #define READ2(sc, a) bus_space_read_2(sc->iot, sc->aud_ioh, a)
119 #define READ4(sc, a) bus_space_read_4(sc->iot, sc->aud_ioh, a)
120 #define WRITE1(sc, a, v) bus_space_write_1(sc->iot, sc->aud_ioh, a, v)
121 #define WRITE2(sc, a, v) bus_space_write_2(sc->iot, sc->aud_ioh, a, v)
122 #define WRITE4(sc, a, v) bus_space_write_4(sc->iot, sc->aud_ioh, a, v)
123
124 /* Debug */
125 #ifdef AUACER_DEBUG
126 #define DPRINTF(l,x) do { if (auacer_debug & (l)) printf x; } while(0)
127 int auacer_debug = 0;
128 #define ALI_DEBUG_CODECIO 0x0001
129 #define ALI_DEBUG_DMA 0x0002
130 #define ALI_DEBUG_INTR 0x0004
131 #define ALI_DEBUG_API 0x0008
132 #define ALI_DEBUG_MIXERAPI 0x0010
133 #else
134 #define DPRINTF(x,y) /* nothing */
135 #endif
136
137 struct cfdriver auacer_cd = {
138 NULL, "auacer", DV_DULL
139 };
140
141 int auacer_match(struct device *, void *, void *);
142 void auacer_attach(struct device *, struct device *, void *);
143 int auacer_activate(struct device *, int);
144 int auacer_intr(void *);
145
146 const struct cfattach auacer_ca = {
147 sizeof(struct auacer_softc), auacer_match, auacer_attach, NULL,
148 auacer_activate
149 };
150
151 int auacer_open(void *, int);
152 void auacer_close(void *);
153 int auacer_set_params(void *, int, int, struct audio_params *,
154 struct audio_params *);
155 int auacer_round_blocksize(void *, int);
156 int auacer_halt_output(void *);
157 int auacer_halt_input(void *);
158 int auacer_set_port(void *, mixer_ctrl_t *);
159 int auacer_get_port(void *, mixer_ctrl_t *);
160 int auacer_query_devinfo(void *, mixer_devinfo_t *);
161 void *auacer_allocm(void *, int, size_t, int, int);
162 void auacer_freem(void *, void *, int);
163 size_t auacer_round_buffersize(void *, int, size_t);
164 int auacer_trigger_output(void *, void *, void *, int, void (*)(void *),
165 void *, struct audio_params *);
166 int auacer_trigger_input(void *, void *, void *, int, void (*)(void *),
167 void *, struct audio_params *);
168
169 int auacer_alloc_cdata(struct auacer_softc *);
170
171 int auacer_allocmem(struct auacer_softc *, size_t, size_t,
172 struct auacer_dma *);
173 int auacer_freemem(struct auacer_softc *, struct auacer_dma *);
174
175 int auacer_set_rate(struct auacer_softc *, int, u_long);
176
177 static void auacer_reset(struct auacer_softc *sc);
178
179 const struct audio_hw_if auacer_hw_if = {
180 .open = auacer_open,
181 .close = auacer_close,
182 .set_params = auacer_set_params,
183 .round_blocksize = auacer_round_blocksize,
184 .halt_output = auacer_halt_output,
185 .halt_input = auacer_halt_input,
186 .set_port = auacer_set_port,
187 .get_port = auacer_get_port,
188 .query_devinfo = auacer_query_devinfo,
189 .allocm = auacer_allocm,
190 .freem = auacer_freem,
191 .round_buffersize = auacer_round_buffersize,
192 .trigger_output = auacer_trigger_output,
193 .trigger_input = auacer_trigger_input,
194 };
195
196 int auacer_attach_codec(void *, struct ac97_codec_if *);
197 int auacer_read_codec(void *, u_int8_t, u_int16_t *);
198 int auacer_write_codec(void *, u_int8_t, u_int16_t);
199 void auacer_reset_codec(void *);
200
201 int
auacer_match(struct device * parent,void * match,void * aux)202 auacer_match(struct device *parent, void *match, void *aux)
203 {
204 return (pci_matchbyid((struct pci_attach_args *)aux, auacer_pci_devices,
205 nitems(auacer_pci_devices)));
206 }
207
208 void
auacer_attach(struct device * parent,struct device * self,void * aux)209 auacer_attach(struct device *parent, struct device *self, void *aux)
210 {
211 struct auacer_softc *sc = (struct auacer_softc *)self;
212 struct pci_attach_args *pa = aux;
213 pci_intr_handle_t ih;
214 bus_size_t aud_size;
215 const char *intrstr;
216
217 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
218 &sc->iot, &sc->aud_ioh, NULL, &aud_size, 0)) {
219 printf(": can't map i/o space\n");
220 return;
221 }
222
223 sc->sc_pc = pa->pa_pc;
224 sc->sc_pt = pa->pa_tag;
225 sc->dmat = pa->pa_dmat;
226
227 sc->sc_dmamap_flags = BUS_DMA_COHERENT; /* XXX remove */
228
229 /* Map and establish the interrupt. */
230 if (pci_intr_map(pa, &ih)) {
231 printf("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
232 return;
233 }
234 intrstr = pci_intr_string(pa->pa_pc, ih);
235 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO | IPL_MPSAFE,
236 auacer_intr, sc, sc->sc_dev.dv_xname);
237 if (sc->sc_ih == NULL) {
238 printf("%s: can't establish interrupt",
239 sc->sc_dev.dv_xname);
240 if (intrstr != NULL)
241 printf(" at %s", intrstr);
242 printf("\n");
243 return;
244 }
245
246 printf(": %s\n", intrstr);
247
248 /* Set up DMA lists. */
249 auacer_alloc_cdata(sc);
250 sc->sc_pcmo.dmalist = sc->sc_cdata->ic_dmalist_pcmo;
251 sc->sc_pcmo.ptr = 0;
252 sc->sc_pcmo.port = ALI_BASE_PO;
253
254 DPRINTF(ALI_DEBUG_DMA, ("auacer_attach: lists %p\n",
255 sc->sc_pcmo.dmalist));
256
257 sc->host_if.arg = sc;
258 sc->host_if.attach = auacer_attach_codec;
259 sc->host_if.read = auacer_read_codec;
260 sc->host_if.write = auacer_write_codec;
261 sc->host_if.reset = auacer_reset_codec;
262
263 if (ac97_attach(&sc->host_if) != 0)
264 return;
265
266 audio_attach_mi(&auacer_hw_if, sc, NULL, &sc->sc_dev);
267
268 auacer_reset(sc);
269 }
270
271 static int
auacer_ready_codec(struct auacer_softc * sc,int mask)272 auacer_ready_codec(struct auacer_softc *sc, int mask)
273 {
274 int count = 0;
275
276 for (count = 0; count < 0x7f; count++) {
277 int val = READ1(sc, ALI_CSPSR);
278 if (val & mask)
279 return 0;
280 }
281
282 printf("auacer_ready_codec: AC97 codec ready timeout.\n");
283 return EBUSY;
284 }
285
286 static int
auacer_sema_codec(struct auacer_softc * sc)287 auacer_sema_codec(struct auacer_softc *sc)
288 {
289 int ttime = 100;
290
291 while (ttime-- && (READ4(sc, ALI_CAS) & ALI_CAS_SEM_BUSY))
292 delay(1);
293 if (!ttime)
294 printf("auacer_sema_codec: timeout\n");
295 return auacer_ready_codec(sc, ALI_CSPSR_CODEC_READY);
296 }
297
298 int
auacer_read_codec(void * v,u_int8_t reg,u_int16_t * val)299 auacer_read_codec(void *v, u_int8_t reg, u_int16_t *val)
300 {
301 struct auacer_softc *sc = v;
302
303 if (auacer_sema_codec(sc))
304 return EIO;
305
306 reg |= ALI_CPR_ADDR_READ;
307 #if 0
308 if (ac97->num)
309 reg |= ALI_CPR_ADDR_SECONDARY;
310 #endif
311 WRITE2(sc, ALI_CPR_ADDR, reg);
312 if (auacer_ready_codec(sc, ALI_CSPSR_READ_OK))
313 return EIO;
314 *val = READ2(sc, ALI_SPR);
315
316 DPRINTF(ALI_DEBUG_CODECIO, ("auacer_read_codec: reg=0x%x val=0x%x\n",
317 reg, *val));
318
319 return 0;
320 }
321
322 int
auacer_write_codec(void * v,u_int8_t reg,u_int16_t val)323 auacer_write_codec(void *v, u_int8_t reg, u_int16_t val)
324 {
325 struct auacer_softc *sc = v;
326
327 DPRINTF(ALI_DEBUG_CODECIO, ("auacer_write_codec: reg=0x%x val=0x%x\n",
328 reg, val));
329
330 if (auacer_sema_codec(sc))
331 return EIO;
332 WRITE2(sc, ALI_CPR, val);
333 #if 0
334 if (ac97->num)
335 reg |= ALI_CPR_ADDR_SECONDARY;
336 #endif
337 WRITE2(sc, ALI_CPR_ADDR, reg);
338 auacer_ready_codec(sc, ALI_CSPSR_WRITE_OK);
339 return 0;
340 }
341
342 int
auacer_attach_codec(void * v,struct ac97_codec_if * cif)343 auacer_attach_codec(void *v, struct ac97_codec_if *cif)
344 {
345 struct auacer_softc *sc = v;
346
347 sc->codec_if = cif;
348 return 0;
349 }
350
351 void
auacer_reset_codec(void * v)352 auacer_reset_codec(void *v)
353 {
354 struct auacer_softc *sc = v;
355 u_int32_t reg;
356 int i = 0;
357
358 reg = READ4(sc, ALI_SCR);
359 if ((reg & 2) == 0) /* Cold required */
360 reg |= 2;
361 else
362 reg |= 1; /* Warm */
363 reg &= ~0x80000000; /* ACLink on */
364 WRITE4(sc, ALI_SCR, reg);
365
366 while (i < 10) {
367 if ((READ4(sc, ALI_INTERRUPTSR) & ALI_INT_GPIO) == 0)
368 break;
369 delay(50000); /* XXX */
370 i++;
371 }
372 if (i == 10) {
373 return;
374 }
375
376 for (i = 0; i < 10; i++) {
377 reg = READ4(sc, ALI_RTSR);
378 if (reg & 0x80) /* primary codec */
379 break;
380 WRITE4(sc, ALI_RTSR, reg | 0x80);
381 delay(50000); /* XXX */
382 }
383 }
384
385 static void
auacer_reset(struct auacer_softc * sc)386 auacer_reset(struct auacer_softc *sc)
387 {
388 WRITE4(sc, ALI_SCR, ALI_SCR_RESET);
389 WRITE4(sc, ALI_FIFOCR1, 0x83838383);
390 WRITE4(sc, ALI_FIFOCR2, 0x83838383);
391 WRITE4(sc, ALI_FIFOCR3, 0x83838383);
392 WRITE4(sc, ALI_INTERFACECR, ALI_IF_PO); /* XXX pcm out only */
393 WRITE4(sc, ALI_INTERRUPTCR, 0x00000000);
394 WRITE4(sc, ALI_INTERRUPTSR, 0x00000000);
395 }
396
397 int
auacer_open(void * v,int flags)398 auacer_open(void *v, int flags)
399 {
400 DPRINTF(ALI_DEBUG_API, ("auacer_open: flags=%d\n", flags));
401 return 0;
402 }
403
404 void
auacer_close(void * v)405 auacer_close(void *v)
406 {
407 DPRINTF(ALI_DEBUG_API, ("auacer_close\n"));
408 }
409
410 int
auacer_set_rate(struct auacer_softc * sc,int mode,u_long srate)411 auacer_set_rate(struct auacer_softc *sc, int mode, u_long srate)
412 {
413 int ret;
414 u_long ratetmp;
415
416 DPRINTF(ALI_DEBUG_API, ("auacer_set_rate: srate=%lu\n", srate));
417
418 ratetmp = srate;
419 if (mode == AUMODE_RECORD)
420 return sc->codec_if->vtbl->set_rate(sc->codec_if,
421 AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
422 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
423 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
424 if (ret)
425 return ret;
426 ratetmp = srate;
427 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
428 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
429 if (ret)
430 return ret;
431 ratetmp = srate;
432 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
433 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
434 return ret;
435 }
436
437 static int
auacer_fixup_rate(int rate)438 auacer_fixup_rate(int rate)
439 {
440 int i;
441 int rates[] = {
442 8000, 11025, 12000, 16000, 22050, 32000, 44100, 48000
443 };
444
445 for (i = 0; i < nitems(rates) - 1; i++)
446 if (rate <= (rates[i] + rates[i+1]) / 2)
447 return (rates[i]);
448 return (rates[i]);
449 }
450
451 int
auacer_set_params(void * v,int setmode,int usemode,struct audio_params * play,struct audio_params * rec)452 auacer_set_params(void *v, int setmode, int usemode, struct audio_params *play,
453 struct audio_params *rec)
454 {
455 struct auacer_softc *sc = v;
456 struct audio_params *p;
457 uint32_t control;
458 int mode;
459
460 DPRINTF(ALI_DEBUG_API, ("auacer_set_params\n"));
461
462 for (mode = AUMODE_RECORD; mode != -1;
463 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
464 if ((setmode & mode) == 0)
465 continue;
466
467 p = mode == AUMODE_PLAY ? play : rec;
468 if (p == NULL)
469 continue;
470
471 p->sample_rate = auacer_fixup_rate(p->sample_rate);
472 p->precision = 16;
473 p->encoding = AUDIO_ENCODING_SLINEAR_LE;
474 if (mode == AUMODE_RECORD) {
475 if (p->channels > 2)
476 p->channels = 2;
477 }
478 p->bps = AUDIO_BPS(p->precision);
479 p->msb = 1;
480
481 if (AC97_IS_FIXED_RATE(sc->codec_if))
482 p->sample_rate = AC97_SINGLE_RATE;
483 else if (auacer_set_rate(sc, mode, p->sample_rate))
484 return EINVAL;
485
486 if (mode == AUMODE_PLAY) {
487 control = READ4(sc, ALI_SCR);
488 control &= ~ALI_SCR_PCM_246_MASK;
489 if (p->channels == 4)
490 control |= ALI_SCR_PCM_4;
491 else if (p->channels == 6)
492 control |= ALI_SCR_PCM_6;
493 WRITE4(sc, ALI_SCR, control);
494 }
495 }
496
497 return (0);
498 }
499
500 int
auacer_round_blocksize(void * v,int blk)501 auacer_round_blocksize(void *v, int blk)
502 {
503 return ((blk + 0x3f) & ~0x3f); /* keep good alignment */
504 }
505
506 static void
auacer_halt(struct auacer_softc * sc,struct auacer_chan * chan)507 auacer_halt(struct auacer_softc *sc, struct auacer_chan *chan)
508 {
509 uint32_t val;
510 uint8_t port = chan->port;
511 uint32_t slot;
512
513 DPRINTF(ALI_DEBUG_API, ("auacer_halt: port=0x%x\n", port));
514
515 chan->intr = 0;
516
517 slot = ALI_PORT2SLOT(port);
518
519 val = READ4(sc, ALI_DMACR);
520 val |= 1 << (slot+16); /* pause */
521 val &= ~(1 << slot); /* no start */
522 WRITE4(sc, ALI_DMACR, val);
523 WRITE1(sc, port + ALI_OFF_CR, 0);
524 while (READ1(sc, port + ALI_OFF_CR))
525 ;
526 /* reset whole DMA things */
527 WRITE1(sc, port + ALI_OFF_CR, ALI_CR_RR);
528 /* clear interrupts */
529 WRITE1(sc, port + ALI_OFF_SR, READ1(sc, port+ALI_OFF_SR) | ALI_SR_W1TC);
530 WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(port));
531 }
532
533 int
auacer_halt_output(void * v)534 auacer_halt_output(void *v)
535 {
536 struct auacer_softc *sc = v;
537
538 DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_output\n"));
539 mtx_enter(&audio_lock);
540 auacer_halt(sc, &sc->sc_pcmo);
541 mtx_leave(&audio_lock);
542 return (0);
543 }
544
545 int
auacer_halt_input(void * v)546 auacer_halt_input(void *v)
547 {
548 DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_input\n"));
549 return (0);
550 }
551
552 int
auacer_set_port(void * v,mixer_ctrl_t * cp)553 auacer_set_port(void *v, mixer_ctrl_t *cp)
554 {
555 struct auacer_softc *sc = v;
556
557 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_set_port\n"));
558 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
559 }
560
561 int
auacer_get_port(void * v,mixer_ctrl_t * cp)562 auacer_get_port(void *v, mixer_ctrl_t *cp)
563 {
564 struct auacer_softc *sc = v;
565
566 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_get_port\n"));
567 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
568 }
569
570 int
auacer_query_devinfo(void * v,mixer_devinfo_t * dp)571 auacer_query_devinfo(void *v, mixer_devinfo_t *dp)
572 {
573 struct auacer_softc *sc = v;
574
575 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_query_devinfo\n"));
576 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dp));
577 }
578
579 void *
auacer_allocm(void * v,int direction,size_t size,int pool,int flags)580 auacer_allocm(void *v, int direction, size_t size, int pool, int flags)
581 {
582 struct auacer_softc *sc = v;
583 struct auacer_dma *p;
584 int error;
585
586 if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
587 return (NULL);
588
589 p = malloc(sizeof(*p), pool, flags | M_ZERO);
590 if (p == NULL)
591 return (NULL);
592
593 error = auacer_allocmem(sc, size, PAGE_SIZE, p);
594 if (error) {
595 free(p, pool, sizeof(*p));
596 return (NULL);
597 }
598
599 p->next = sc->sc_dmas;
600 sc->sc_dmas = p;
601
602 return (KERNADDR(p));
603 }
604
605 void
auacer_freem(void * v,void * ptr,int pool)606 auacer_freem(void *v, void *ptr, int pool)
607 {
608 struct auacer_softc *sc = v;
609 struct auacer_dma *p, **pp;
610
611 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
612 if (KERNADDR(p) == ptr) {
613 auacer_freemem(sc, p);
614 *pp = p->next;
615 free(p, pool, sizeof(*p));
616 return;
617 }
618 }
619 }
620
621 size_t
auacer_round_buffersize(void * v,int direction,size_t size)622 auacer_round_buffersize(void *v, int direction, size_t size)
623 {
624
625 if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
626 size = ALI_DMALIST_MAX * ALI_DMASEG_MAX;
627
628 return size;
629 }
630
631 static void
auacer_add_entry(struct auacer_chan * chan)632 auacer_add_entry(struct auacer_chan *chan)
633 {
634 struct auacer_dmalist *q;
635
636 q = &chan->dmalist[chan->ptr];
637
638 DPRINTF(ALI_DEBUG_INTR,
639 ("auacer_add_entry: %p = %x @ 0x%x\n",
640 q, chan->blksize / 2, chan->p));
641
642 q->base = htole32(chan->p);
643 q->len = htole32((chan->blksize / ALI_SAMPLE_SIZE) | ALI_DMAF_IOC);
644 chan->p += chan->blksize;
645 if (chan->p >= chan->end)
646 chan->p = chan->start;
647
648 if (++chan->ptr >= ALI_DMALIST_MAX)
649 chan->ptr = 0;
650 }
651
652 static void
auacer_upd_chan(struct auacer_softc * sc,struct auacer_chan * chan)653 auacer_upd_chan(struct auacer_softc *sc, struct auacer_chan *chan)
654 {
655 uint32_t sts;
656 uint32_t civ;
657
658 sts = READ2(sc, chan->port + ALI_OFF_SR);
659 /* intr ack */
660 WRITE2(sc, chan->port + ALI_OFF_SR, sts & ALI_SR_W1TC);
661 WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(chan->port));
662
663 DPRINTF(ALI_DEBUG_INTR, ("auacer_upd_chan: sts=0x%x\n", sts));
664
665 if (sts & ALI_SR_DMA_INT_FIFO) {
666 printf("%s: fifo underrun # %u\n",
667 sc->sc_dev.dv_xname, ++chan->fifoe);
668 }
669
670 civ = READ1(sc, chan->port + ALI_OFF_CIV);
671
672 DPRINTF(ALI_DEBUG_INTR,("auacer_intr: civ=%u ptr=%u\n",civ,chan->ptr));
673
674 /* XXX */
675 while (chan->ptr != civ) {
676 auacer_add_entry(chan);
677 }
678
679 WRITE1(sc, chan->port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
680
681 while (chan->ack != civ) {
682 if (chan->intr) {
683 DPRINTF(ALI_DEBUG_INTR,("auacer_upd_chan: callback\n"));
684 chan->intr(chan->arg);
685 }
686 chan->ack++;
687 if (chan->ack >= ALI_DMALIST_MAX)
688 chan->ack = 0;
689 }
690 }
691
692 int
auacer_intr(void * v)693 auacer_intr(void *v)
694 {
695 struct auacer_softc *sc = v;
696 int ret, intrs;
697
698 mtx_enter(&audio_lock);
699 intrs = READ4(sc, ALI_INTERRUPTSR);
700 DPRINTF(ALI_DEBUG_INTR, ("auacer_intr: intrs=0x%x\n", intrs));
701
702 ret = 0;
703 if (intrs & ALI_INT_PCMOUT) {
704 auacer_upd_chan(sc, &sc->sc_pcmo);
705 ret++;
706 }
707 mtx_leave(&audio_lock);
708 return ret != 0;
709 }
710
711 static void
auacer_setup_chan(struct auacer_softc * sc,struct auacer_chan * chan,uint32_t start,uint32_t size,uint32_t blksize,void (* intr)(void *),void * arg)712 auacer_setup_chan(struct auacer_softc *sc, struct auacer_chan *chan,
713 uint32_t start, uint32_t size, uint32_t blksize, void (*intr)(void *),
714 void *arg)
715 {
716 uint32_t port, slot;
717 uint32_t offs, val;
718
719 chan->start = start;
720 chan->ptr = 0;
721 chan->p = chan->start;
722 chan->end = chan->start + size;
723 chan->blksize = blksize;
724 chan->ack = 0;
725 chan->intr = intr;
726 chan->arg = arg;
727
728 auacer_add_entry(chan);
729 auacer_add_entry(chan);
730
731 port = chan->port;
732 slot = ALI_PORT2SLOT(port);
733
734 WRITE1(sc, port + ALI_OFF_CIV, 0);
735 WRITE1(sc, port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
736 offs = (char *)chan->dmalist - (char *)sc->sc_cdata;
737 WRITE4(sc, port + ALI_OFF_BDBAR, sc->sc_cddma + offs);
738 WRITE1(sc, port + ALI_OFF_CR,
739 ALI_CR_IOCE | ALI_CR_FEIE | ALI_CR_LVBIE | ALI_CR_RPBM);
740 val = READ4(sc, ALI_DMACR);
741 val &= ~(1 << (slot+16)); /* no pause */
742 val |= 1 << slot; /* start */
743 WRITE4(sc, ALI_DMACR, val);
744 }
745
746 int
auacer_trigger_output(void * v,void * start,void * end,int blksize,void (* intr)(void *),void * arg,struct audio_params * param)747 auacer_trigger_output(void *v, void *start, void *end, int blksize,
748 void (*intr)(void *), void *arg, struct audio_params *param)
749 {
750 struct auacer_softc *sc = v;
751 struct auacer_dma *p;
752 uint32_t size;
753
754 DPRINTF(ALI_DEBUG_DMA,
755 ("auacer_trigger_output(%p, %p, %d, %p, %p, %p)\n",
756 start, end, blksize, intr, arg, param));
757
758 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
759 ;
760 if (!p) {
761 printf("auacer_trigger_output: bad addr %p\n", start);
762 return (EINVAL);
763 }
764
765 size = (char *)end - (char *)start;
766 mtx_enter(&audio_lock);
767 auacer_setup_chan(sc, &sc->sc_pcmo, DMAADDR(p), size, blksize,
768 intr, arg);
769 mtx_leave(&audio_lock);
770 return 0;
771 }
772
773 int
auacer_trigger_input(void * v,void * start,void * end,int blksize,void (* intr)(void *),void * arg,struct audio_params * param)774 auacer_trigger_input(void *v, void *start, void *end, int blksize,
775 void (*intr)(void *), void *arg, struct audio_params *param)
776 {
777 return (EINVAL);
778 }
779
780 int
auacer_allocmem(struct auacer_softc * sc,size_t size,size_t align,struct auacer_dma * p)781 auacer_allocmem(struct auacer_softc *sc, size_t size, size_t align,
782 struct auacer_dma *p)
783 {
784 int error;
785
786 p->size = size;
787 error = bus_dmamem_alloc(sc->dmat, p->size, align, 0, p->segs,
788 nitems(p->segs), &p->nsegs, BUS_DMA_NOWAIT);
789 if (error)
790 return (error);
791
792 error = bus_dmamem_map(sc->dmat, p->segs, p->nsegs, p->size, &p->addr,
793 BUS_DMA_NOWAIT | sc->sc_dmamap_flags);
794 if (error)
795 goto free;
796
797 error = bus_dmamap_create(sc->dmat, p->size, 1, p->size, 0,
798 BUS_DMA_NOWAIT, &p->map);
799 if (error)
800 goto unmap;
801
802 error = bus_dmamap_load(sc->dmat, p->map, p->addr, p->size, NULL,
803 BUS_DMA_NOWAIT);
804 if (error)
805 goto destroy;
806 return (0);
807
808 destroy:
809 bus_dmamap_destroy(sc->dmat, p->map);
810 unmap:
811 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
812 free:
813 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
814 return (error);
815 }
816
817 int
auacer_freemem(struct auacer_softc * sc,struct auacer_dma * p)818 auacer_freemem(struct auacer_softc *sc, struct auacer_dma *p)
819 {
820
821 bus_dmamap_unload(sc->dmat, p->map);
822 bus_dmamap_destroy(sc->dmat, p->map);
823 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
824 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
825 return (0);
826 }
827
828 int
auacer_alloc_cdata(struct auacer_softc * sc)829 auacer_alloc_cdata(struct auacer_softc *sc)
830 {
831 bus_dma_segment_t seg;
832 int error, rseg;
833
834 /*
835 * Allocate the control data structure, and create and load the
836 * DMA map for it.
837 */
838 if ((error = bus_dmamem_alloc(sc->dmat, sizeof(struct auacer_cdata),
839 PAGE_SIZE, 0, &seg, 1, &rseg, 0)) != 0) {
840 printf("%s: unable to allocate control data, error = %d\n",
841 sc->sc_dev.dv_xname, error);
842 goto fail_0;
843 }
844
845 if ((error = bus_dmamem_map(sc->dmat, &seg, rseg,
846 sizeof(struct auacer_cdata), (caddr_t *) &sc->sc_cdata,
847 sc->sc_dmamap_flags)) != 0) {
848 printf("%s: unable to map control data, error = %d\n",
849 sc->sc_dev.dv_xname, error);
850 goto fail_1;
851 }
852
853 if ((error = bus_dmamap_create(sc->dmat, sizeof(struct auacer_cdata), 1,
854 sizeof(struct auacer_cdata), 0, 0, &sc->sc_cddmamap)) != 0) {
855 printf("%s: unable to create control data DMA map, "
856 "error = %d\n", sc->sc_dev.dv_xname, error);
857 goto fail_2;
858 }
859
860 if ((error = bus_dmamap_load(sc->dmat, sc->sc_cddmamap, sc->sc_cdata,
861 sizeof(struct auacer_cdata), NULL, 0)) != 0) {
862 printf("%s: unable to load control data DMA map, error = %d\n",
863 sc->sc_dev.dv_xname, error);
864 goto fail_3;
865 }
866
867 return (0);
868
869 fail_3:
870 bus_dmamap_destroy(sc->dmat, sc->sc_cddmamap);
871 fail_2:
872 bus_dmamem_unmap(sc->dmat, (caddr_t) sc->sc_cdata,
873 sizeof(struct auacer_cdata));
874 fail_1:
875 bus_dmamem_free(sc->dmat, &seg, rseg);
876 fail_0:
877 return (error);
878 }
879
880 int
auacer_activate(struct device * self,int act)881 auacer_activate(struct device *self, int act)
882 {
883 struct auacer_softc *sc = (struct auacer_softc *)self;
884
885 if (act == DVACT_RESUME)
886 ac97_resume(&sc->host_if, sc->codec_if);
887 return (config_activate_children(self, act));
888 }
889