auacer.c revision 1.13 1 /* $NetBSD: auacer.c,v 1.13 2006/09/24 03:53:09 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Acer Labs M5455 audio driver
41 *
42 * Acer provides data sheets after signing an NDA, so this is guess work.
43 * The chip behaves somewhat like the Intel i8x0, so this driver
44 * is loosely based on the auich driver. Additional information taken from
45 * the ALSA intel8x0.c driver (which handles M5455 as well).
46 *
47 * As an historical note one can observe that the auich driver borrows
48 * lot from the first NetBSD PCI audio driver, the eap driver. But this
49 * is not attributed anywhere.
50 */
51
52
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: auacer.c,v 1.13 2006/09/24 03:53:09 jmcneill Exp $");
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/fcntl.h>
62 #include <sys/proc.h>
63
64 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
65
66 #include <dev/pci/pcidevs.h>
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/auacerreg.h>
69
70 #include <sys/audioio.h>
71 #include <dev/audio_if.h>
72 #include <dev/mulaw.h>
73 #include <dev/auconv.h>
74
75 #include <machine/bus.h>
76
77 #include <dev/ic/ac97reg.h>
78 #include <dev/ic/ac97var.h>
79
80 struct auacer_dma {
81 bus_dmamap_t map;
82 caddr_t addr;
83 bus_dma_segment_t segs[1];
84 int nsegs;
85 size_t size;
86 struct auacer_dma *next;
87 };
88
89 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
90 #define KERNADDR(p) ((void *)((p)->addr))
91
92 struct auacer_cdata {
93 struct auacer_dmalist ic_dmalist_pcmo[ALI_DMALIST_MAX];
94 };
95
96 struct auacer_chan {
97 uint32_t ptr;
98 uint32_t start, p, end;
99 uint32_t blksize, fifoe;
100 uint32_t ack;
101 uint32_t port;
102 struct auacer_dmalist *dmalist;
103 void (*intr)(void *);
104 void *arg;
105 };
106
107 struct auacer_softc {
108 struct device sc_dev;
109 void *sc_ih;
110
111 audio_device_t sc_audev;
112
113 bus_space_tag_t iot;
114 bus_space_handle_t mix_ioh;
115 bus_space_handle_t aud_ioh;
116 bus_dma_tag_t dmat;
117
118 struct ac97_codec_if *codec_if;
119 struct ac97_host_if host_if;
120
121 /* DMA scatter-gather lists. */
122 bus_dmamap_t sc_cddmamap;
123 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
124
125 struct auacer_cdata *sc_cdata;
126
127 struct auacer_chan sc_pcmo;
128
129 struct auacer_dma *sc_dmas;
130
131 pci_chipset_tag_t sc_pc;
132 pcitag_t sc_pt;
133
134 int sc_dmamap_flags;
135
136 /* Power Management */
137 void *sc_powerhook;
138 int sc_suspend;
139
140 #define AUACER_NFORMATS 3
141 struct audio_format sc_formats[AUACER_NFORMATS];
142 struct audio_encoding_set *sc_encodings;
143 };
144
145 #define READ1(sc, a) bus_space_read_1(sc->iot, sc->aud_ioh, a)
146 #define READ2(sc, a) bus_space_read_2(sc->iot, sc->aud_ioh, a)
147 #define READ4(sc, a) bus_space_read_4(sc->iot, sc->aud_ioh, a)
148 #define WRITE1(sc, a, v) bus_space_write_1(sc->iot, sc->aud_ioh, a, v)
149 #define WRITE2(sc, a, v) bus_space_write_2(sc->iot, sc->aud_ioh, a, v)
150 #define WRITE4(sc, a, v) bus_space_write_4(sc->iot, sc->aud_ioh, a, v)
151
152 /* Debug */
153 #ifdef AUACER_DEBUG
154 #define DPRINTF(l,x) do { if (auacer_debug & (l)) printf x; } while(0)
155 int auacer_debug = 0;
156 #define ALI_DEBUG_CODECIO 0x0001
157 #define ALI_DEBUG_DMA 0x0002
158 #define ALI_DEBUG_INTR 0x0004
159 #define ALI_DEBUG_API 0x0008
160 #define ALI_DEBUG_MIXERAPI 0x0010
161 #else
162 #define DPRINTF(x,y) /* nothing */
163 #endif
164
165 static int auacer_intr(void *);
166
167 static int auacer_query_encoding(void *, struct audio_encoding *);
168 static int auacer_set_params(void *, int, int, audio_params_t *,
169 audio_params_t *, stream_filter_list_t *,
170 stream_filter_list_t *);
171 static int auacer_round_blocksize(void *, int, int,
172 const audio_params_t *);
173 static int auacer_halt_output(void *);
174 static int auacer_halt_input(void *);
175 static int auacer_getdev(void *, struct audio_device *);
176 static int auacer_set_port(void *, mixer_ctrl_t *);
177 static int auacer_get_port(void *, mixer_ctrl_t *);
178 static int auacer_query_devinfo(void *, mixer_devinfo_t *);
179 static void *auacer_allocm(void *, int, size_t, struct malloc_type *, int);
180 static void auacer_freem(void *, void *, struct malloc_type *);
181 static size_t auacer_round_buffersize(void *, int, size_t);
182 static paddr_t auacer_mappage(void *, void *, off_t, int);
183 static int auacer_get_props(void *);
184 static int auacer_trigger_output(void *, void *, void *, int,
185 void (*)(void *), void *,
186 const audio_params_t *);
187 static int auacer_trigger_input(void *, void *, void *, int,
188 void (*)(void *), void *,
189 const audio_params_t *);
190
191 static int auacer_alloc_cdata(struct auacer_softc *);
192
193 static int auacer_allocmem(struct auacer_softc *, size_t, size_t,
194 struct auacer_dma *);
195 static int auacer_freemem(struct auacer_softc *, struct auacer_dma *);
196
197 static void auacer_powerhook(int, void *);
198 static int auacer_set_rate(struct auacer_softc *, int, u_int);
199
200 static void auacer_reset(struct auacer_softc *sc);
201
202 static struct audio_hw_if auacer_hw_if = {
203 NULL, /* open */
204 NULL, /* close */
205 NULL, /* drain */
206 auacer_query_encoding,
207 auacer_set_params,
208 auacer_round_blocksize,
209 NULL, /* commit_setting */
210 NULL, /* init_output */
211 NULL, /* init_input */
212 NULL, /* start_output */
213 NULL, /* start_input */
214 auacer_halt_output,
215 auacer_halt_input,
216 NULL, /* speaker_ctl */
217 auacer_getdev,
218 NULL, /* getfd */
219 auacer_set_port,
220 auacer_get_port,
221 auacer_query_devinfo,
222 auacer_allocm,
223 auacer_freem,
224 auacer_round_buffersize,
225 auacer_mappage,
226 auacer_get_props,
227 auacer_trigger_output,
228 auacer_trigger_input,
229 NULL, /* dev_ioctl */
230 NULL, /* powerstate */
231 };
232
233 #define AUACER_FORMATS_4CH 1
234 #define AUACER_FORMATS_6CH 2
235 static const struct audio_format auacer_formats[AUACER_NFORMATS] = {
236 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
237 2, AUFMT_STEREO, 0, {8000, 48000}},
238 {NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
239 4, AUFMT_SURROUND4, 0, {8000, 48000}},
240 {NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
241 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
242 };
243
244 static int auacer_attach_codec(void *, struct ac97_codec_if *);
245 static int auacer_read_codec(void *, uint8_t, uint16_t *);
246 static int auacer_write_codec(void *, uint8_t, uint16_t);
247 static int auacer_reset_codec(void *);
248
249 static int
250 auacer_match(struct device *parent, struct cfdata *match, void *aux)
251 {
252 struct pci_attach_args *pa;
253
254 pa = (struct pci_attach_args *)aux;
255 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI &&
256 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALI_M5455)
257 return 1;
258 return 0;
259 }
260
261 static void
262 auacer_attach(struct device *parent, struct device *self, void *aux)
263 {
264 struct auacer_softc *sc;
265 struct pci_attach_args *pa;
266 pci_intr_handle_t ih;
267 bus_size_t aud_size;
268 pcireg_t v;
269 const char *intrstr;
270 int i;
271
272 sc = (struct auacer_softc *)self;
273 pa = aux;
274 aprint_normal(": Acer Labs M5455 Audio controller\n");
275
276 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->iot,
277 &sc->aud_ioh, NULL, &aud_size)) {
278 aprint_error(": can't map i/o space\n");
279 return;
280 }
281
282 sc->sc_pc = pa->pa_pc;
283 sc->sc_pt = pa->pa_tag;
284 sc->dmat = pa->pa_dmat;
285
286 sc->sc_dmamap_flags = BUS_DMA_COHERENT; /* XXX remove */
287
288 /* enable bus mastering */
289 v = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
290 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
291 v | PCI_COMMAND_MASTER_ENABLE);
292
293 /* Map and establish the interrupt. */
294 if (pci_intr_map(pa, &ih)) {
295 aprint_error("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
296 return;
297 }
298 intrstr = pci_intr_string(pa->pa_pc, ih);
299 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO,
300 auacer_intr, sc);
301 if (sc->sc_ih == NULL) {
302 aprint_error("%s: can't establish interrupt",
303 sc->sc_dev.dv_xname);
304 if (intrstr != NULL)
305 aprint_normal(" at %s", intrstr);
306 aprint_normal("\n");
307 return;
308 }
309 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
310
311 strlcpy(sc->sc_audev.name, "M5455 AC97", MAX_AUDIO_DEV_LEN);
312 snprintf(sc->sc_audev.version, MAX_AUDIO_DEV_LEN,
313 "0x%02x", PCI_REVISION(pa->pa_class));
314 strlcpy(sc->sc_audev.config, sc->sc_dev.dv_xname, MAX_AUDIO_DEV_LEN);
315
316 /* Set up DMA lists. */
317 auacer_alloc_cdata(sc);
318 sc->sc_pcmo.dmalist = sc->sc_cdata->ic_dmalist_pcmo;
319 sc->sc_pcmo.ptr = 0;
320 sc->sc_pcmo.port = ALI_BASE_PO;
321
322 DPRINTF(ALI_DEBUG_DMA, ("auacer_attach: lists %p\n",
323 sc->sc_pcmo.dmalist));
324
325 sc->host_if.arg = sc;
326 sc->host_if.attach = auacer_attach_codec;
327 sc->host_if.read = auacer_read_codec;
328 sc->host_if.write = auacer_write_codec;
329 sc->host_if.reset = auacer_reset_codec;
330
331 if (ac97_attach(&sc->host_if, self) != 0)
332 return;
333
334 /* setup audio_format */
335 memcpy(sc->sc_formats, auacer_formats, sizeof(auacer_formats));
336 if (!AC97_IS_4CH(sc->codec_if))
337 AUFMT_INVALIDATE(&sc->sc_formats[AUACER_FORMATS_4CH]);
338 if (!AC97_IS_6CH(sc->codec_if))
339 AUFMT_INVALIDATE(&sc->sc_formats[AUACER_FORMATS_6CH]);
340 if (AC97_IS_FIXED_RATE(sc->codec_if)) {
341 for (i = 0; i < AUACER_NFORMATS; i++) {
342 sc->sc_formats[i].frequency_type = 1;
343 sc->sc_formats[i].frequency[0] = 48000;
344 }
345 }
346
347 if (0 != auconv_create_encodings(sc->sc_formats, AUACER_NFORMATS,
348 &sc->sc_encodings)) {
349 return;
350 }
351
352 /* Watch for power change */
353 sc->sc_suspend = PWR_RESUME;
354 sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
355 auacer_powerhook, sc);
356
357 audio_attach_mi(&auacer_hw_if, sc, &sc->sc_dev);
358
359 auacer_reset(sc);
360 }
361
362 CFATTACH_DECL(auacer, sizeof(struct auacer_softc),
363 auacer_match, auacer_attach, NULL, NULL);
364
365 static int
366 auacer_ready_codec(struct auacer_softc *sc, int mask)
367 {
368 int count;
369
370 for (count = 0; count < 0x7f; count++) {
371 int val = READ1(sc, ALI_CSPSR);
372 if (val & mask)
373 return 0;
374 }
375
376 aprint_normal("auacer_ready_codec: AC97 codec ready timeout.\n");
377 return EBUSY;
378 }
379
380 static int
381 auacer_sema_codec(struct auacer_softc *sc)
382 {
383 int ttime;
384
385 ttime = 100;
386 while (ttime-- && (READ4(sc, ALI_CAS) & ALI_CAS_SEM_BUSY))
387 delay(1);
388 if (!ttime)
389 aprint_normal("auacer_sema_codec: timeout\n");
390 return auacer_ready_codec(sc, ALI_CSPSR_CODEC_READY);
391 }
392
393 static int
394 auacer_read_codec(void *v, uint8_t reg, uint16_t *val)
395 {
396 struct auacer_softc *sc;
397
398 sc = v;
399 if (auacer_sema_codec(sc))
400 return EIO;
401
402 reg |= ALI_CPR_ADDR_READ;
403 #if 0
404 if (ac97->num)
405 reg |= ALI_CPR_ADDR_SECONDARY;
406 #endif
407 WRITE2(sc, ALI_CPR_ADDR, reg);
408 if (auacer_ready_codec(sc, ALI_CSPSR_READ_OK))
409 return EIO;
410 *val = READ2(sc, ALI_SPR);
411
412 DPRINTF(ALI_DEBUG_CODECIO, ("auacer_read_codec: reg=0x%x val=0x%x\n",
413 reg, *val));
414
415 return 0;
416 }
417
418 int
419 auacer_write_codec(void *v, uint8_t reg, uint16_t val)
420 {
421 struct auacer_softc *sc;
422
423 DPRINTF(ALI_DEBUG_CODECIO, ("auacer_write_codec: reg=0x%x val=0x%x\n",
424 reg, val));
425 sc = v;
426 if (auacer_sema_codec(sc))
427 return EIO;
428 WRITE2(sc, ALI_CPR, val);
429 #if 0
430 if (ac97->num)
431 reg |= ALI_CPR_ADDR_SECONDARY;
432 #endif
433 WRITE2(sc, ALI_CPR_ADDR, reg);
434 auacer_ready_codec(sc, ALI_CSPSR_WRITE_OK);
435 return 0;
436 }
437
438 static int
439 auacer_attach_codec(void *v, struct ac97_codec_if *cif)
440 {
441 struct auacer_softc *sc;
442
443 sc = v;
444 sc->codec_if = cif;
445 return 0;
446 }
447
448 static int
449 auacer_reset_codec(void *v)
450 {
451 struct auacer_softc *sc;
452 uint32_t reg;
453 int i;
454
455 sc = v;
456 i = 0;
457 reg = READ4(sc, ALI_SCR);
458 if ((reg & 2) == 0) /* Cold required */
459 reg |= 2;
460 else
461 reg |= 1; /* Warm */
462 reg &= ~0x80000000; /* ACLink on */
463 WRITE4(sc, ALI_SCR, reg);
464
465 while (i < 10) {
466 if ((READ4(sc, ALI_INTERRUPTSR) & ALI_INT_GPIO) == 0)
467 break;
468 delay(50000); /* XXX */
469 i++;
470 }
471 if (i == 10) {
472 return EIO;
473 }
474
475 for (i = 0; i < 10; i++) {
476 reg = READ4(sc, ALI_RTSR);
477 if (reg & 0x80) /* primary codec */
478 break;
479 WRITE4(sc, ALI_RTSR, reg | 0x80);
480 delay(50000); /* XXX */
481 }
482
483 return 0;
484 }
485
486 static void
487 auacer_reset(struct auacer_softc *sc)
488 {
489 WRITE4(sc, ALI_SCR, ALI_SCR_RESET);
490 WRITE4(sc, ALI_FIFOCR1, 0x83838383);
491 WRITE4(sc, ALI_FIFOCR2, 0x83838383);
492 WRITE4(sc, ALI_FIFOCR3, 0x83838383);
493 WRITE4(sc, ALI_INTERFACECR, ALI_IF_PO); /* XXX pcm out only */
494 WRITE4(sc, ALI_INTERRUPTCR, 0x00000000);
495 WRITE4(sc, ALI_INTERRUPTSR, 0x00000000);
496 }
497
498 static int
499 auacer_query_encoding(void *v, struct audio_encoding *aep)
500 {
501 struct auacer_softc *sc;
502
503 DPRINTF(ALI_DEBUG_API, ("auacer_query_encoding\n"));
504 sc = v;
505 return auconv_query_encoding(sc->sc_encodings, aep);
506 }
507
508 static int
509 auacer_set_rate(struct auacer_softc *sc, int mode, u_int srate)
510 {
511 int ret;
512 u_int ratetmp;
513
514 DPRINTF(ALI_DEBUG_API, ("auacer_set_rate: srate=%u\n", srate));
515
516 ratetmp = srate;
517 if (mode == AUMODE_RECORD)
518 return sc->codec_if->vtbl->set_rate(sc->codec_if,
519 AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
520 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
521 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
522 if (ret)
523 return ret;
524 ratetmp = srate;
525 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
526 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
527 if (ret)
528 return ret;
529 ratetmp = srate;
530 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
531 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
532 return ret;
533 }
534
535 static int
536 auacer_set_params(void *v, int setmode, int usemode, audio_params_t *play,
537 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
538 {
539 struct auacer_softc *sc;
540 struct audio_params *p;
541 stream_filter_list_t *fil;
542 uint32_t control;
543 int mode, index;
544
545 DPRINTF(ALI_DEBUG_API, ("auacer_set_params\n"));
546 sc = v;
547 for (mode = AUMODE_RECORD; mode != -1;
548 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
549 if ((setmode & mode) == 0)
550 continue;
551
552 p = mode == AUMODE_PLAY ? play : rec;
553 if (p == NULL)
554 continue;
555
556 if ((p->sample_rate != 8000) &&
557 (p->sample_rate != 11025) &&
558 (p->sample_rate != 12000) &&
559 (p->sample_rate != 16000) &&
560 (p->sample_rate != 22050) &&
561 (p->sample_rate != 24000) &&
562 (p->sample_rate != 32000) &&
563 (p->sample_rate != 44100) &&
564 (p->sample_rate != 48000))
565 return (EINVAL);
566
567 fil = mode == AUMODE_PLAY ? pfil : rfil;
568 index = auconv_set_converter(sc->sc_formats, AUACER_NFORMATS,
569 mode, p, TRUE, fil);
570 if (index < 0)
571 return EINVAL;
572 if (fil->req_size > 0)
573 p = &fil->filters[0].param;
574 /* p points HW encoding */
575 if (sc->sc_formats[index].frequency_type != 1
576 && auacer_set_rate(sc, mode, p->sample_rate))
577 return EINVAL;
578 if (mode == AUMODE_PLAY) {
579 control = READ4(sc, ALI_SCR);
580 control &= ~ALI_SCR_PCM_246_MASK;
581 if (p->channels == 4)
582 control |= ALI_SCR_PCM_4;
583 else if (p->channels == 6)
584 control |= ALI_SCR_PCM_6;
585 WRITE4(sc, ALI_SCR, control);
586 }
587 }
588
589 return (0);
590 }
591
592 static int
593 auacer_round_blocksize(void *v, int blk, int mode, const audio_params_t *param)
594 {
595
596 return blk & ~0x3f; /* keep good alignment */
597 }
598
599 static void
600 auacer_halt(struct auacer_softc *sc, struct auacer_chan *chan)
601 {
602 uint32_t val;
603 uint8_t port;
604 uint32_t slot;
605
606 port = chan->port;
607 DPRINTF(ALI_DEBUG_API, ("auacer_halt: port=0x%x\n", port));
608 chan->intr = 0;
609
610 slot = ALI_PORT2SLOT(port);
611
612 val = READ4(sc, ALI_DMACR);
613 val |= 1 << (slot+16); /* pause */
614 val &= ~(1 << slot); /* no start */
615 WRITE4(sc, ALI_DMACR, val);
616 WRITE1(sc, port + ALI_OFF_CR, 0);
617 while (READ1(sc, port + ALI_OFF_CR))
618 ;
619 /* reset whole DMA things */
620 WRITE1(sc, port + ALI_OFF_CR, ALI_CR_RR);
621 /* clear interrupts */
622 WRITE1(sc, port + ALI_OFF_SR, READ1(sc, port+ALI_OFF_SR) | ALI_SR_W1TC);
623 WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(port));
624 }
625
626 static int
627 auacer_halt_output(void *v)
628 {
629 struct auacer_softc *sc;
630
631 DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_output\n"));
632 sc = v;
633 auacer_halt(sc, &sc->sc_pcmo);
634
635 return 0;
636 }
637
638 static int
639 auacer_halt_input(void *v)
640 {
641 /*struct auacer_softc *sc = v;*/
642
643 DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_input\n"));
644
645 return 0;
646 }
647
648 static int
649 auacer_getdev(void *v, struct audio_device *adp)
650 {
651 struct auacer_softc *sc;
652
653 DPRINTF(ALI_DEBUG_API, ("auacer_getdev\n"));
654 sc = v;
655 *adp = sc->sc_audev;
656 return 0;
657 }
658
659 static int
660 auacer_set_port(void *v, mixer_ctrl_t *cp)
661 {
662 struct auacer_softc *sc;
663
664 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_set_port\n"));
665 sc = v;
666 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
667 }
668
669 static int
670 auacer_get_port(void *v, mixer_ctrl_t *cp)
671 {
672 struct auacer_softc *sc;
673
674 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_get_port\n"));
675 sc = v;
676 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
677 }
678
679 static int
680 auacer_query_devinfo(void *v, mixer_devinfo_t *dp)
681 {
682 struct auacer_softc *sc;
683
684 DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_query_devinfo\n"));
685 sc = v;
686 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dp);
687 }
688
689 static void *
690 auacer_allocm(void *v, int direction, size_t size, struct malloc_type *pool,
691 int flags)
692 {
693 struct auacer_softc *sc;
694 struct auacer_dma *p;
695 int error;
696
697 if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
698 return NULL;
699
700 p = malloc(sizeof(*p), pool, flags | M_ZERO);
701 if (p == NULL)
702 return NULL;
703 sc = v;
704 error = auacer_allocmem(sc, size, 0, p);
705 if (error) {
706 free(p, pool);
707 return NULL;
708 }
709
710 p->next = sc->sc_dmas;
711 sc->sc_dmas = p;
712
713 return KERNADDR(p);
714 }
715
716 static void
717 auacer_freem(void *v, void *ptr, struct malloc_type *pool)
718 {
719 struct auacer_softc *sc;
720 struct auacer_dma *p, **pp;
721
722 sc = v;
723 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
724 if (KERNADDR(p) == ptr) {
725 auacer_freemem(sc, p);
726 *pp = p->next;
727 free(p, pool);
728 return;
729 }
730 }
731 }
732
733 static size_t
734 auacer_round_buffersize(void *v, int direction, size_t size)
735 {
736
737 if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
738 size = ALI_DMALIST_MAX * ALI_DMASEG_MAX;
739
740 return size;
741 }
742
743 static paddr_t
744 auacer_mappage(void *v, void *mem, off_t off, int prot)
745 {
746 struct auacer_softc *sc;
747 struct auacer_dma *p;
748
749 if (off < 0)
750 return -1;
751 sc = v;
752 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
753 continue;
754 if (p == NULL)
755 return -1;
756 return bus_dmamem_mmap(sc->dmat, p->segs, p->nsegs,
757 off, prot, BUS_DMA_WAITOK);
758 }
759
760 static int
761 auacer_get_props(void *v)
762 {
763 struct auacer_softc *sc;
764 int props;
765
766 sc = v;
767 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
768 /*
769 * Even if the codec is fixed-rate, set_param() succeeds for any sample
770 * rate because of aurateconv. Applications can't know what rate the
771 * device can process in the case of mmap().
772 */
773 if (!AC97_IS_FIXED_RATE(sc->codec_if))
774 props |= AUDIO_PROP_MMAP;
775 return props;
776 }
777
778 static void
779 auacer_add_entry(struct auacer_chan *chan)
780 {
781 struct auacer_dmalist *q;
782
783 q = &chan->dmalist[chan->ptr];
784
785 DPRINTF(ALI_DEBUG_INTR,
786 ("auacer_add_entry: %p = %x @ 0x%x\n",
787 q, chan->blksize / 2, chan->p));
788
789 q->base = htole32(chan->p);
790 q->len = htole32((chan->blksize / ALI_SAMPLE_SIZE) | ALI_DMAF_IOC);
791 chan->p += chan->blksize;
792 if (chan->p >= chan->end)
793 chan->p = chan->start;
794
795 if (++chan->ptr >= ALI_DMALIST_MAX)
796 chan->ptr = 0;
797 }
798
799 static void
800 auacer_upd_chan(struct auacer_softc *sc, struct auacer_chan *chan)
801 {
802 uint32_t sts;
803 uint32_t civ;
804
805 sts = READ2(sc, chan->port + ALI_OFF_SR);
806 /* intr ack */
807 WRITE2(sc, chan->port + ALI_OFF_SR, sts & ALI_SR_W1TC);
808 WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(chan->port));
809
810 DPRINTF(ALI_DEBUG_INTR, ("auacer_upd_chan: sts=0x%x\n", sts));
811
812 if (sts & ALI_SR_DMA_INT_FIFO) {
813 printf("%s: fifo underrun # %u\n",
814 sc->sc_dev.dv_xname, ++chan->fifoe);
815 }
816
817 civ = READ1(sc, chan->port + ALI_OFF_CIV);
818
819 DPRINTF(ALI_DEBUG_INTR,("auacer_intr: civ=%u ptr=%u\n",civ,chan->ptr));
820
821 /* XXX */
822 while (chan->ptr != civ) {
823 auacer_add_entry(chan);
824 }
825
826 WRITE1(sc, chan->port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
827
828 while (chan->ack != civ) {
829 if (chan->intr) {
830 DPRINTF(ALI_DEBUG_INTR,("auacer_upd_chan: callback\n"));
831 chan->intr(chan->arg);
832 }
833 chan->ack++;
834 if (chan->ack >= ALI_DMALIST_MAX)
835 chan->ack = 0;
836 }
837 }
838
839 static int
840 auacer_intr(void *v)
841 {
842 struct auacer_softc *sc;
843 int ret, intrs;
844
845 sc = v;
846 intrs = READ4(sc, ALI_INTERRUPTSR);
847 DPRINTF(ALI_DEBUG_INTR, ("auacer_intr: intrs=0x%x\n", intrs));
848
849 ret = 0;
850 if (intrs & ALI_INT_PCMOUT) {
851 auacer_upd_chan(sc, &sc->sc_pcmo);
852 ret++;
853 }
854
855 return ret != 0;
856 }
857
858 static void
859 auacer_setup_chan(struct auacer_softc *sc, struct auacer_chan *chan,
860 uint32_t start, uint32_t size, uint32_t blksize,
861 void (*intr)(void *), void *arg)
862 {
863 uint32_t port, slot;
864 uint32_t offs, val;
865
866 chan->start = start;
867 chan->ptr = 0;
868 chan->p = chan->start;
869 chan->end = chan->start + size;
870 chan->blksize = blksize;
871 chan->ack = 0;
872 chan->intr = intr;
873 chan->arg = arg;
874
875 auacer_add_entry(chan);
876 auacer_add_entry(chan);
877
878 port = chan->port;
879 slot = ALI_PORT2SLOT(port);
880
881 WRITE1(sc, port + ALI_OFF_CIV, 0);
882 WRITE1(sc, port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
883 offs = (char *)chan->dmalist - (char *)sc->sc_cdata;
884 WRITE4(sc, port + ALI_OFF_BDBAR, sc->sc_cddma + offs);
885 WRITE1(sc, port + ALI_OFF_CR,
886 ALI_CR_IOCE | ALI_CR_FEIE | ALI_CR_LVBIE | ALI_CR_RPBM);
887 val = READ4(sc, ALI_DMACR);
888 val &= ~(1 << (slot+16)); /* no pause */
889 val |= 1 << slot; /* start */
890 WRITE4(sc, ALI_DMACR, val);
891 }
892
893 static int
894 auacer_trigger_output(void *v, void *start, void *end, int blksize,
895 void (*intr)(void *), void *arg, const audio_params_t *param)
896 {
897 struct auacer_softc *sc;
898 struct auacer_dma *p;
899 uint32_t size;
900
901 DPRINTF(ALI_DEBUG_DMA,
902 ("auacer_trigger_output(%p, %p, %d, %p, %p, %p)\n",
903 start, end, blksize, intr, arg, param));
904 sc = v;
905 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
906 continue;
907 if (!p) {
908 printf("auacer_trigger_output: bad addr %p\n", start);
909 return (EINVAL);
910 }
911
912 size = (char *)end - (char *)start;
913 auacer_setup_chan(sc, &sc->sc_pcmo, DMAADDR(p), size, blksize,
914 intr, arg);
915
916 return 0;
917 }
918
919 static int
920 auacer_trigger_input(void *v, void *start, void *end, int blksize,
921 void (*intr)(void *), void *arg,
922 const audio_params_t *param)
923 {
924 return EINVAL;
925 }
926
927 static int
928 auacer_allocmem(struct auacer_softc *sc, size_t size, size_t align,
929 struct auacer_dma *p)
930 {
931 int error;
932
933 p->size = size;
934 error = bus_dmamem_alloc(sc->dmat, p->size, align, 0,
935 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
936 &p->nsegs, BUS_DMA_NOWAIT);
937 if (error)
938 return error;
939
940 error = bus_dmamem_map(sc->dmat, p->segs, p->nsegs, p->size,
941 &p->addr, BUS_DMA_NOWAIT|sc->sc_dmamap_flags);
942 if (error)
943 goto free;
944
945 error = bus_dmamap_create(sc->dmat, p->size, 1, p->size,
946 0, BUS_DMA_NOWAIT, &p->map);
947 if (error)
948 goto unmap;
949
950 error = bus_dmamap_load(sc->dmat, p->map, p->addr, p->size, NULL,
951 BUS_DMA_NOWAIT);
952 if (error)
953 goto destroy;
954 return (0);
955
956 destroy:
957 bus_dmamap_destroy(sc->dmat, p->map);
958 unmap:
959 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
960 free:
961 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
962 return error;
963 }
964
965 static int
966 auacer_freemem(struct auacer_softc *sc, struct auacer_dma *p)
967 {
968
969 bus_dmamap_unload(sc->dmat, p->map);
970 bus_dmamap_destroy(sc->dmat, p->map);
971 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
972 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
973 return 0;
974 }
975
976 static int
977 auacer_alloc_cdata(struct auacer_softc *sc)
978 {
979 bus_dma_segment_t seg;
980 int error, rseg;
981
982 /*
983 * Allocate the control data structure, and create and load the
984 * DMA map for it.
985 */
986 if ((error = bus_dmamem_alloc(sc->dmat,
987 sizeof(struct auacer_cdata),
988 PAGE_SIZE, 0, &seg, 1, &rseg, 0)) != 0) {
989 printf("%s: unable to allocate control data, error = %d\n",
990 sc->sc_dev.dv_xname, error);
991 goto fail_0;
992 }
993
994 if ((error = bus_dmamem_map(sc->dmat, &seg, rseg,
995 sizeof(struct auacer_cdata),
996 (caddr_t *) &sc->sc_cdata,
997 sc->sc_dmamap_flags)) != 0) {
998 printf("%s: unable to map control data, error = %d\n",
999 sc->sc_dev.dv_xname, error);
1000 goto fail_1;
1001 }
1002
1003 if ((error = bus_dmamap_create(sc->dmat, sizeof(struct auacer_cdata), 1,
1004 sizeof(struct auacer_cdata), 0, 0,
1005 &sc->sc_cddmamap)) != 0) {
1006 printf("%s: unable to create control data DMA map, "
1007 "error = %d\n", sc->sc_dev.dv_xname, error);
1008 goto fail_2;
1009 }
1010
1011 if ((error = bus_dmamap_load(sc->dmat, sc->sc_cddmamap,
1012 sc->sc_cdata, sizeof(struct auacer_cdata),
1013 NULL, 0)) != 0) {
1014 printf("%s: unable to load control data DMA map, "
1015 "error = %d\n", sc->sc_dev.dv_xname, error);
1016 goto fail_3;
1017 }
1018
1019 return 0;
1020
1021 fail_3:
1022 bus_dmamap_destroy(sc->dmat, sc->sc_cddmamap);
1023 fail_2:
1024 bus_dmamem_unmap(sc->dmat, (caddr_t) sc->sc_cdata,
1025 sizeof(struct auacer_cdata));
1026 fail_1:
1027 bus_dmamem_free(sc->dmat, &seg, rseg);
1028 fail_0:
1029 return error;
1030 }
1031
1032 static void
1033 auacer_powerhook(int why, void *addr)
1034 {
1035 struct auacer_softc *sc;
1036
1037 sc = (struct auacer_softc *)addr;
1038 switch (why) {
1039 case PWR_SUSPEND:
1040 case PWR_STANDBY:
1041 /* Power down */
1042 DPRINTF(1, ("%s: power down\n", sc->sc_dev.dv_xname));
1043 sc->sc_suspend = why;
1044 break;
1045
1046 case PWR_RESUME:
1047 /* Wake up */
1048 DPRINTF(1, ("%s: power resume\n", sc->sc_dev.dv_xname));
1049 if (sc->sc_suspend == PWR_RESUME) {
1050 printf("%s: resume without suspend.\n",
1051 sc->sc_dev.dv_xname);
1052 sc->sc_suspend = why;
1053 return;
1054 }
1055 sc->sc_suspend = why;
1056 auacer_reset_codec(sc);
1057 delay(1000);
1058 sc->codec_if->vtbl->restore_ports(sc->codec_if);
1059 break;
1060
1061 case PWR_SOFTSUSPEND:
1062 case PWR_SOFTSTANDBY:
1063 case PWR_SOFTRESUME:
1064 break;
1065 }
1066 }
1067