Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / esa.c
blob55abf4fdaf265ecfc081b227ea3c4cab38b03b6b
1 /* $NetBSD: esa.c,v 1.52 2009/11/26 15:17:09 njoly Exp $ */
3 /*
4 * Copyright (c) 2001-2008 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
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. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
29 * ESS Allegro-1 / Maestro3 Audio Driver
31 * Based on the FreeBSD maestro3 driver and the NetBSD eap driver.
32 * Original driver by Don Kim.
34 * The list management code could possibly be written better, but what
35 * we have right now does the job nicely. Thanks to Zach Brown <zab@zabbo.net>
36 * and Andrew MacDonald <amac@epsilon.yi.org> for helping me debug the
37 * problems with the original list management code present in the Linux
38 * driver.
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: esa.c,v 1.52 2009/11/26 15:17:09 njoly Exp $");
44 #include <sys/types.h>
45 #include <sys/errno.h>
46 #include <sys/null.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/conf.h>
52 #include <sys/exec.h>
53 #include <sys/select.h>
54 #include <sys/audioio.h>
56 #include <sys/bus.h>
57 #include <sys/intr.h>
59 #include <dev/pci/pcidevs.h>
60 #include <dev/pci/pcivar.h>
62 #include <dev/audio_if.h>
63 #include <dev/mulaw.h>
64 #include <dev/auconv.h>
65 #include <dev/ic/ac97var.h>
66 #include <dev/ic/ac97reg.h>
68 #include <dev/pci/esareg.h>
69 #include <dev/pci/esadsp.h>
70 #include <dev/pci/esavar.h>
72 #define PCI_CBIO 0x10
74 #define ESA_DAC_DATA 0x1100
76 enum {
77 ESS_ALLEGRO1,
78 ESS_MAESTRO3
81 static const struct esa_card_type {
82 uint16_t pci_vendor_id;
83 uint16_t pci_product_id;
84 int type;
85 int delay1, delay2;
86 } esa_card_types[] = {
87 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ALLEGRO1,
88 ESS_ALLEGRO1, 50, 800 },
89 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3,
90 ESS_MAESTRO3, 20, 500 },
91 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2,
92 ESS_MAESTRO3, 20, 500 },
93 { 0, 0, 0, 0, 0 }
96 static struct audio_device esa_device = {
97 "ESS Allegro",
98 "",
99 "esa"
102 static int esa_match(device_t, cfdata_t, void *);
103 static void esa_attach(device_t, device_t, void *);
104 static int esa_detach(device_t, int);
105 static void esa_childdet(device_t, device_t);
107 /* audio(9) functions */
108 static int esa_query_encoding(void *, struct audio_encoding *);
109 static int esa_set_params(void *, int, int, audio_params_t *,
110 audio_params_t *, stream_filter_list_t *,
111 stream_filter_list_t *);
112 static int esa_round_blocksize(void *, int, int,
113 const audio_params_t *);
114 static int esa_commit_settings(void *);
115 static int esa_halt_output(void *);
116 static int esa_halt_input(void *);
117 static int esa_set_port(void *, mixer_ctrl_t *);
118 static int esa_get_port(void *, mixer_ctrl_t *);
119 static int esa_query_devinfo(void *, mixer_devinfo_t *);
120 static void * esa_malloc(void *, int, size_t, struct malloc_type *,
121 int);
122 static void esa_free(void *, void *, struct malloc_type *);
123 static int esa_getdev(void *, struct audio_device *);
124 static size_t esa_round_buffersize(void *, int, size_t);
125 static int esa_get_props(void *);
126 static int esa_trigger_output(void *, void *, void *, int,
127 void (*)(void *), void *,
128 const audio_params_t *);
129 static int esa_trigger_input(void *, void *, void *, int,
130 void (*)(void *), void *,
131 const audio_params_t *);
133 static int esa_intr(void *);
134 static int esa_allocmem(struct esa_softc *, size_t, size_t,
135 struct esa_dma *);
136 static int esa_freemem(struct esa_softc *, struct esa_dma *);
137 static paddr_t esa_mappage(void *, void *, off_t, int);
139 /* Supporting subroutines */
140 static uint16_t esa_read_assp(struct esa_softc *, uint16_t, uint16_t);
141 static void esa_write_assp(struct esa_softc *, uint16_t, uint16_t,
142 uint16_t);
143 static int esa_init_codec(struct esa_softc *);
144 static int esa_attach_codec(void *, struct ac97_codec_if *);
145 static int esa_read_codec(void *, uint8_t, uint16_t *);
146 static int esa_write_codec(void *, uint8_t, uint16_t);
147 static int esa_reset_codec(void *);
148 static enum ac97_host_flags esa_flags_codec(void *);
149 static int esa_wait(struct esa_softc *);
150 static int esa_init(struct esa_softc *);
151 static void esa_config(struct esa_softc *);
152 static uint8_t esa_assp_halt(struct esa_softc *);
153 static void esa_codec_reset(struct esa_softc *);
154 static int esa_amp_enable(struct esa_softc *);
155 static void esa_enable_interrupts(struct esa_softc *);
156 static uint32_t esa_get_pointer(struct esa_softc *,
157 struct esa_channel *);
159 /* list management */
160 static int esa_add_list(struct esa_voice *, struct esa_list *,
161 uint16_t, int);
162 static void esa_remove_list(struct esa_voice *, struct esa_list *,
163 int);
165 /* power management */
166 static bool esa_suspend(device_t, pmf_qual_t);
167 static bool esa_resume(device_t, pmf_qual_t);
170 #define ESA_NENCODINGS 8
171 static audio_encoding_t esa_encoding[ESA_NENCODINGS] = {
172 { 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 },
173 { 1, AudioEmulaw, AUDIO_ENCODING_ULAW, 8,
174 AUDIO_ENCODINGFLAG_EMULATED },
175 { 2, AudioEalaw, AUDIO_ENCODING_ALAW, 8, AUDIO_ENCODINGFLAG_EMULATED },
176 { 3, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8,
177 AUDIO_ENCODINGFLAG_EMULATED }, /* XXX: Are you sure? */
178 { 4, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 },
179 { 5, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16,
180 AUDIO_ENCODINGFLAG_EMULATED },
181 { 6, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16,
182 AUDIO_ENCODINGFLAG_EMULATED },
183 { 7, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16,
184 AUDIO_ENCODINGFLAG_EMULATED }
187 #define ESA_NFORMATS 4
188 static const struct audio_format esa_formats[ESA_NFORMATS] = {
189 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
190 2, AUFMT_STEREO, 0, {ESA_MINRATE, ESA_MAXRATE}},
191 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
192 1, AUFMT_MONAURAL, 0, {ESA_MINRATE, ESA_MAXRATE}},
193 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
194 2, AUFMT_STEREO, 0, {ESA_MINRATE, ESA_MAXRATE}},
195 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
196 1, AUFMT_MONAURAL, 0, {ESA_MINRATE, ESA_MAXRATE}},
199 static const struct audio_hw_if esa_hw_if = {
200 NULL, /* open */
201 NULL, /* close */
202 NULL, /* drain */
203 esa_query_encoding,
204 esa_set_params,
205 esa_round_blocksize,
206 esa_commit_settings,
207 NULL, /* init_output */
208 NULL, /* init_input */
209 NULL, /* start_output */
210 NULL, /* start_input */
211 esa_halt_output,
212 esa_halt_input,
213 NULL, /* speaker_ctl */
214 esa_getdev,
215 NULL, /* getfd */
216 esa_set_port,
217 esa_get_port,
218 esa_query_devinfo,
219 esa_malloc,
220 esa_free,
221 esa_round_buffersize,
222 esa_mappage,
223 esa_get_props,
224 esa_trigger_output,
225 esa_trigger_input,
226 NULL, /* dev_ioctl */
227 NULL, /* powerstate */
230 CFATTACH_DECL2_NEW(esa, sizeof(struct esa_softc), esa_match, esa_attach,
231 esa_detach, NULL, NULL, esa_childdet);
234 * audio(9) functions
237 static int
238 esa_query_encoding(void *hdl, struct audio_encoding *ae)
241 if (ae->index < 0 || ae->index >= ESA_NENCODINGS)
242 return EINVAL;
243 *ae = esa_encoding[ae->index];
245 return 0;
248 static int
249 esa_set_params(void *hdl, int setmode, int usemode,
250 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
251 stream_filter_list_t *rfil)
253 struct esa_voice *vc;
254 struct esa_channel *ch;
255 struct audio_params *p;
256 stream_filter_list_t *fil;
257 int mode, i;
259 vc = hdl;
260 for (mode = AUMODE_RECORD; mode != -1;
261 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
262 if ((setmode & mode) == 0)
263 continue;
265 switch (mode) {
266 case AUMODE_PLAY:
267 p = play;
268 ch = &vc->play;
269 fil = pfil;
270 break;
271 case AUMODE_RECORD:
272 p = rec;
273 ch = &vc->rec;
274 fil = rfil;
275 break;
276 default:
277 return EINVAL;
280 if (p->sample_rate < ESA_MINRATE ||
281 p->sample_rate > ESA_MAXRATE ||
282 (p->precision != 8 && p->precision != 16) ||
283 (p->channels < 1 || p->channels > 2))
284 return EINVAL;
286 i = auconv_set_converter(esa_formats, ESA_NFORMATS,
287 mode, p, FALSE, fil);
288 if (i < 0)
289 return EINVAL;
290 if (fil->req_size > 0)
291 p = &fil->filters[0].param;
292 ch->mode = *p;
295 return 0;
298 static int
299 esa_commit_settings(void *hdl)
301 struct esa_voice *vc;
302 struct esa_softc *sc;
303 const audio_params_t *p;
304 const audio_params_t *r;
305 uint32_t data;
306 uint32_t freq;
307 int data_bytes;
309 vc = hdl;
310 sc = device_private(vc->parent);
311 p = &vc->play.mode;
312 r = &vc->rec.mode;
313 data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
314 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
315 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
316 &~ 255;
317 /* playback */
318 vc->play.data_offset = ESA_DAC_DATA + (data_bytes * vc->index);
319 if (p->channels == 1)
320 data = 1;
321 else
322 data = 0;
323 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
324 vc->play.data_offset + ESA_SRC3_MODE_OFFSET,
325 data);
326 if (p->precision == 8)
327 data = 1;
328 else
329 data = 0;
330 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
331 vc->play.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
332 data);
333 if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) {
334 freq--;
336 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
337 vc->play.data_offset + ESA_CDATA_FREQUENCY, freq);
339 /* recording */
340 vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * vc->index) +
341 (data_bytes / 2);
342 if (r->channels == 1)
343 data = 1;
344 else
345 data = 0;
346 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
347 vc->rec.data_offset + ESA_SRC3_MODE_OFFSET,
348 data);
349 if (r->precision == 8)
350 data = 1;
351 else
352 data = 0;
353 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
354 vc->rec.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
355 data);
356 if ((freq = ((r->sample_rate << 15) + 24000) / 48000) != 0) {
357 freq--;
359 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
360 vc->rec.data_offset + ESA_CDATA_FREQUENCY, freq);
362 return 0;
365 static int
366 esa_round_blocksize(void *hdl, int bs, int mode,
367 const audio_params_t *param)
370 return bs & ~0x20; /* Be conservative; align to 32 bytes */
373 static int
374 esa_halt_output(void *hdl)
376 struct esa_voice *vc;
377 struct esa_softc *sc;
378 bus_space_tag_t iot;
379 bus_space_handle_t ioh;
380 uint16_t data;
382 vc = hdl;
383 sc = device_private(vc->parent);
384 iot = sc->sc_iot;
385 ioh = sc->sc_ioh;
386 if (vc->play.active == 0)
387 return 0;
389 vc->play.active = 0;
391 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
392 ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0);
394 sc->sc_ntimers--;
395 if (sc->sc_ntimers == 0) {
396 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
397 ESA_KDATA_TIMER_COUNT_RELOAD, 0);
398 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
399 ESA_KDATA_TIMER_COUNT_CURRENT, 0);
400 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
401 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
402 data & ~ESA_CLKRUN_GEN_ENABLE);
405 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
406 ESA_KDATA_MIXER_TASK_NUMBER,
407 sc->mixer_list.indexmap[vc->index]);
408 /* remove ourselves from the packed lists */
409 esa_remove_list(vc, &sc->mixer_list, vc->index);
410 esa_remove_list(vc, &sc->dma_list, vc->index);
411 esa_remove_list(vc, &sc->msrc_list, vc->index);
413 return 0;
416 static int
417 esa_halt_input(void *hdl)
419 struct esa_voice *vc;
420 struct esa_softc *sc;
421 bus_space_tag_t iot;
422 bus_space_handle_t ioh;
423 uint32_t data;
425 vc = hdl;
426 sc = device_private(vc->parent);
427 iot = sc->sc_iot;
428 ioh = sc->sc_ioh;
429 if (vc->rec.active == 0)
430 return 0;
432 vc->rec.active = 0;
434 sc->sc_ntimers--;
435 if (sc->sc_ntimers == 0) {
436 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
437 ESA_KDATA_TIMER_COUNT_RELOAD, 0);
438 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
439 ESA_KDATA_TIMER_COUNT_CURRENT, 0);
440 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
441 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
442 data & ~ESA_CLKRUN_GEN_ENABLE);
445 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset +
446 ESA_CDATA_INSTANCE_READY, 0);
447 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
450 /* remove ourselves from the packed lists */
451 esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES);
452 esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES);
453 esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES);
455 return 0;
458 static void *
459 esa_malloc(void *hdl, int direction, size_t size,
460 struct malloc_type *type, int flags)
462 struct esa_voice *vc;
463 struct esa_softc *sc;
464 struct esa_dma *p;
465 int error;
467 p = malloc(sizeof(*p), type, flags);
468 if (p == NULL)
469 return NULL;
470 vc = hdl;
471 sc = device_private(vc->parent);
472 error = esa_allocmem(sc, size, 16, p);
473 if (error) {
474 free(p, type);
475 aprint_error_dev(sc->sc_dev,
476 "%s: not enough memory\n", __func__);
477 return 0;
479 p->next = vc->dma;
480 vc->dma = p;
482 return KERNADDR(p);
485 static void
486 esa_free(void *hdl, void *addr, struct malloc_type *type)
488 struct esa_voice *vc;
489 struct esa_softc *sc;
490 struct esa_dma *p;
491 struct esa_dma **pp;
493 vc = hdl;
494 sc = device_private(vc->parent);
495 for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next)
496 if (KERNADDR(p) == addr) {
497 esa_freemem(sc, p);
498 *pp = p->next;
499 free(p, type);
500 return;
504 static int
505 esa_getdev(void *hdl, struct audio_device *ret)
508 *ret = esa_device;
509 return 0;
512 static int
513 esa_set_port(void *hdl, mixer_ctrl_t *mc)
515 struct esa_voice *vc;
516 struct esa_softc *sc;
518 vc = hdl;
519 sc = device_private(vc->parent);
520 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc);
523 static int
524 esa_get_port(void *hdl, mixer_ctrl_t *mc)
526 struct esa_voice *vc;
527 struct esa_softc *sc;
529 vc = hdl;
530 sc = device_private(vc->parent);
531 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc);
534 static int
535 esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
537 struct esa_voice *vc;
538 struct esa_softc *sc;
540 vc = hdl;
541 sc = device_private(vc->parent);
542 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, di);
545 static size_t
546 esa_round_buffersize(void *hdl, int direction, size_t bufsize)
549 return bufsize;
552 static int
553 esa_get_props(void *hdl)
556 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
559 static int
560 esa_trigger_output(void *hdl, void *start, void *end, int blksize,
561 void (*intr)(void *), void *intrarg, const audio_params_t *param)
563 struct esa_voice *vc;
564 struct esa_softc *sc;
565 struct esa_dma *p;
566 bus_space_tag_t iot;
567 bus_space_handle_t ioh;
568 size_t size;
569 uint32_t data, bufaddr, i;
570 int data_bytes, dac_data, dsp_in_size;
571 int dsp_out_size, dsp_in_buf, dsp_out_buf;
573 vc = hdl;
574 sc = device_private(vc->parent);
575 iot = sc->sc_iot;
576 ioh = sc->sc_ioh;
577 data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
578 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
579 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255;
580 dac_data = ESA_DAC_DATA + (data_bytes * vc->index);
581 dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
582 dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
583 dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
584 dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
586 if (vc->play.active)
587 return EINVAL;
589 for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
590 continue;
591 if (p == NULL) {
592 aprint_error_dev(sc->sc_dev, "%s: bad addr %p\n", __func__,
593 start);
594 return EINVAL;
597 vc->play.active = 1;
598 vc->play.intr = intr;
599 vc->play.arg = intrarg;
600 vc->play.pos = 0;
601 vc->play.count = 0;
602 vc->play.buf = start;
603 vc->play.bufsize = size = (size_t)(((char *)end - (char *)start));
604 vc->play.blksize = blksize;
605 bufaddr = DMAADDR(p);
606 vc->play.start = bufaddr;
608 #define LO(x) ((x) & 0x0000ffff)
609 #define HI(x) ((x) >> 16)
611 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
612 ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
613 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
614 ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
615 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
616 ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
617 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
618 ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
619 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
620 ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
621 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
622 ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
624 /* DSP buffers */
625 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
626 ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
627 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
628 ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
629 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
630 ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
631 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
632 ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
633 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
634 ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
635 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
636 ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
637 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
638 ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
639 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
640 ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
642 /* Some per-client initializers */
643 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
644 ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8);
645 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
646 ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
647 /* Enable or disable low-pass filter? (0xff if rate > 45000) */
648 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
649 ESA_SRC3_DIRECTION_OFFSET + 22,
650 vc->play.mode.sample_rate > 45000 ? 0xff : 0);
651 /* Tell it which way DMA is going */
652 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
653 ESA_CDATA_DMA_CONTROL,
654 ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
655 ESA_DMAC_BLOCKF_SELECTOR);
657 /* Set an armload of static initializers */
658 for (i = 0; i < (sizeof(esa_playvals) / sizeof(esa_playvals[0])); i++)
659 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
660 esa_playvals[i].addr, esa_playvals[i].val);
662 /* Put us in the packed task lists */
663 esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT,
664 vc->index);
665 esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT,
666 vc->index);
667 esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT,
668 vc->index);
669 #undef LO
670 #undef HI
672 sc->sc_ntimers++;
674 if (sc->sc_ntimers == 1) {
675 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
676 ESA_KDATA_TIMER_COUNT_RELOAD, 240);
677 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
678 ESA_KDATA_TIMER_COUNT_CURRENT, 240);
679 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
680 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
681 data | ESA_CLKRUN_GEN_ENABLE);
684 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
685 ESA_CDATA_INSTANCE_READY, 1);
686 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
687 ESA_KDATA_MIXER_TASK_NUMBER,
688 sc->mixer_list.indexmap[vc->index]);
690 return 0;
693 static int
694 esa_trigger_input(void *hdl, void *start, void *end, int blksize,
695 void (*intr)(void *), void *intrarg, const audio_params_t *param)
697 struct esa_voice *vc;
698 struct esa_softc *sc;
699 struct esa_dma *p;
700 bus_space_tag_t iot;
701 bus_space_handle_t ioh;
702 uint32_t data, bufaddr, i;
703 size_t size;
704 int data_bytes, adc_data, dsp_in_size;
705 int dsp_out_size, dsp_in_buf, dsp_out_buf;
707 vc = hdl;
708 sc = device_private(vc->parent);
709 iot = sc->sc_iot;
710 ioh = sc->sc_ioh;
711 data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
712 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
713 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255;
714 adc_data = ESA_DAC_DATA + (data_bytes * vc->index) + (data_bytes / 2);
715 dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
716 dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
717 dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
718 dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
720 vc->rec.data_offset = adc_data;
722 /* We only support 1 recording channel */
723 if (vc->index > 0)
724 return ENODEV;
726 if (vc->rec.active)
727 return EINVAL;
729 for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
730 continue;
731 if (p == NULL) {
732 aprint_error_dev(sc->sc_dev, "%s: bad addr %p\n",
733 __func__, start);
734 return EINVAL;
737 vc->rec.active = 1;
738 vc->rec.intr = intr;
739 vc->rec.arg = intrarg;
740 vc->rec.pos = 0;
741 vc->rec.count = 0;
742 vc->rec.buf = start;
743 vc->rec.bufsize = size = (size_t)(((char *)end - (char *)start));
744 vc->rec.blksize = blksize;
745 bufaddr = DMAADDR(p);
746 vc->rec.start = bufaddr;
748 #define LO(x) ((x) & 0x0000ffff)
749 #define HI(x) ((x) >> 16)
751 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
752 ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
753 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
754 ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
755 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
756 ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
757 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
758 ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
759 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
760 ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
761 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
762 ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
764 /* DSP buffers */
765 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
766 ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
767 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
768 ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
769 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
770 ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
771 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
772 ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
773 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
774 ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
775 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
776 ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
777 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
778 ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
779 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
780 ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
782 /* Some per-client initializers */
783 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
784 ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8);
785 /* Tell it which way DMA is going */
786 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
787 ESA_CDATA_DMA_CONTROL,
788 ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT +
789 ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR);
791 /* Set an armload of static initializers */
792 for (i = 0; i < (sizeof(esa_recvals) / sizeof(esa_recvals[0])); i++)
793 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
794 esa_recvals[i].addr, esa_recvals[i].val);
796 /* Put us in the packed task lists */
797 esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT,
798 vc->index + ESA_NUM_VOICES);
799 esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT,
800 vc->index + ESA_NUM_VOICES);
801 esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT,
802 vc->index + ESA_NUM_VOICES);
803 #undef LO
804 #undef HI
806 sc->sc_ntimers++;
807 if (sc->sc_ntimers == 1) {
808 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
809 ESA_KDATA_TIMER_COUNT_RELOAD, 240);
810 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
811 ESA_KDATA_TIMER_COUNT_CURRENT, 240);
812 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
813 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
814 data | ESA_CLKRUN_GEN_ENABLE);
817 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
818 ESA_CDATA_INSTANCE_READY, 1);
819 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
822 return 0;
825 /* Interrupt handler */
826 static int
827 esa_intr(void *hdl)
829 struct esa_softc *sc;
830 struct esa_voice *vc;
831 bus_space_tag_t iot;
832 bus_space_handle_t ioh;
833 uint8_t status;
834 uint32_t pos;
835 uint32_t diff;
836 uint32_t blksize;
837 int i;
839 sc = hdl;
840 iot = sc->sc_iot;
841 ioh = sc->sc_ioh;
843 status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
844 if (status == 0xff)
845 return 0;
847 /* ack the interrupt */
848 bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status);
850 if (status & ESA_HV_INT_PENDING) {
851 uint8_t event;
853 aprint_normal_dev(sc->sc_dev, "hardware volume interrupt\n");
854 event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
855 switch(event) {
856 case 0xaa: /* volume up */
857 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
858 break;
859 case 0x66: /* volume down */
860 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
861 break;
862 case 0x88: /* mute */
863 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
864 break;
865 default:
866 aprint_normal_dev(sc->sc_dev,
867 "unknown hwvol event 0x%02x\n", event);
868 break;
870 bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
873 if ((status & ESA_ASSP_INT_PENDING) == 0 ||
874 (bus_space_read_1(iot, ioh,
875 ESA_ASSP_CONTROL_B) & ESA_STOP_ASSP_CLOCK) != 0 ||
876 (bus_space_read_1(iot, ioh,
877 ESA_ASSP_HOST_INT_STATUS) & ESA_DSP2HOST_REQ_TIMER) == 0)
878 return 1;
880 bus_space_write_1(iot, ioh, ESA_ASSP_HOST_INT_STATUS,
881 ESA_DSP2HOST_REQ_TIMER);
883 for (i = 0; i < ESA_NUM_VOICES; i++) {
884 vc = &sc->voice[i];
886 if (vc->play.active) {
887 pos = esa_get_pointer(sc, &vc->play) % vc->play.bufsize;
888 diff = (vc->play.bufsize + pos - vc->play.pos) %
889 vc->play.bufsize;
891 vc->play.pos = pos;
892 vc->play.count += diff;
893 blksize = vc->play.blksize;
895 while (vc->play.count >= blksize) {
896 vc->play.count -= blksize;
897 (*vc->play.intr)(vc->play.arg);
901 if (vc->rec.active) {
902 pos = esa_get_pointer(sc, &vc->rec) % vc->rec.bufsize;
903 diff = (vc->rec.bufsize + pos - vc->rec.pos) %
904 vc->rec.bufsize;
906 vc->rec.pos = pos;
907 vc->rec.count += diff;
908 blksize = vc->rec.blksize;
910 while (vc->rec.count >= blksize) {
911 vc->rec.count -= blksize;
912 (*vc->rec.intr)(vc->rec.arg);
917 return 1;
920 static int
921 esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
922 struct esa_dma *p)
924 int error;
926 p->size = size;
927 error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
928 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
929 &p->nsegs, BUS_DMA_NOWAIT);
930 if (error)
931 return error;
933 error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
934 &p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
935 if (error)
936 goto free;
938 error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
939 BUS_DMA_NOWAIT, &p->map);
940 if (error)
941 goto unmap;
943 error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
944 BUS_DMA_NOWAIT);
945 if (error)
946 goto destroy;
948 return 0;
950 destroy:
951 bus_dmamap_destroy(sc->sc_dmat, p->map);
952 unmap:
953 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
954 free:
955 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
957 return error;
960 static int
961 esa_freemem(struct esa_softc *sc, struct esa_dma *p)
964 bus_dmamap_unload(sc->sc_dmat, p->map);
965 bus_dmamap_destroy(sc->sc_dmat, p->map);
966 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
967 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
969 return 0;
973 * Supporting Subroutines
976 static int
977 esa_match(device_t dev, cfdata_t match, void *aux)
979 struct pci_attach_args *pa;
981 pa = (struct pci_attach_args *)aux;
982 switch (PCI_VENDOR(pa->pa_id)) {
983 case PCI_VENDOR_ESSTECH:
984 switch(PCI_PRODUCT(pa->pa_id)) {
985 case PCI_PRODUCT_ESSTECH_ALLEGRO1:
986 case PCI_PRODUCT_ESSTECH_MAESTRO3:
987 case PCI_PRODUCT_ESSTECH_MAESTRO3_2:
988 return 1;
992 return 0;
995 static void
996 esa_attach(device_t parent, device_t self, void *aux)
998 struct esa_softc *sc;
999 struct pci_attach_args *pa;
1000 pcitag_t tag;
1001 pci_chipset_tag_t pc;
1002 pci_intr_handle_t ih;
1003 const struct esa_card_type *card;
1004 const char *intrstr;
1005 uint32_t data;
1006 char devinfo[256];
1007 int revision, len;
1008 int i;
1009 int error;
1011 sc = device_private(self);
1012 pa = (struct pci_attach_args *)aux;
1013 tag = pa->pa_tag;
1014 pc = pa->pa_pc;
1015 aprint_naive(": Audio controller\n");
1017 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
1018 revision = PCI_REVISION(pa->pa_class);
1019 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
1021 for (card = esa_card_types; card->pci_vendor_id; card++)
1022 if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
1023 PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
1024 sc->type = card->type;
1025 sc->delay1 = card->delay1;
1026 sc->delay2 = card->delay2;
1027 break;
1030 data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
1031 data |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
1032 | PCI_COMMAND_MASTER_ENABLE);
1033 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
1035 /* Map I/O register */
1036 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
1037 &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
1038 aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
1039 return;
1042 /* Initialize softc */
1043 sc->sc_dev = self;
1044 sc->sc_tag = tag;
1045 sc->sc_pct = pc;
1046 sc->sc_dmat = pa->pa_dmat;
1048 /* Map and establish an interrupt */
1049 if (pci_intr_map(pa, &ih)) {
1050 aprint_error_dev(sc->sc_dev, "can't map interrupt\n");
1051 return;
1053 intrstr = pci_intr_string(pc, ih);
1054 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, esa_intr, sc);
1055 if (sc->sc_ih == NULL) {
1056 aprint_error_dev(sc->sc_dev, "can't establish interrupt");
1057 if (intrstr != NULL)
1058 aprint_error(" at %s", intrstr);
1059 aprint_error("\n");
1060 return;
1062 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
1064 /* power up chip */
1065 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
1066 pci_activate_null)) && error != EOPNOTSUPP) {
1067 aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
1068 return;
1071 /* Init chip */
1072 if (esa_init(sc) == -1) {
1073 aprint_error_dev(sc->sc_dev,
1074 "esa_attach: unable to initialize the card\n");
1075 return;
1078 /* create suspend save area */
1079 len = sizeof(uint16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1080 + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1081 sc->savemem = (uint16_t *)malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1082 if (sc->savemem == NULL) {
1083 aprint_error_dev(sc->sc_dev,
1084 "unable to allocate suspend buffer\n");
1085 return;
1089 * Every card I've seen has had their channels swapped with respect
1090 * to the mixer. Ie:
1091 * $ mixerctl -w outputs.master=0,191
1092 * Would result in the _right_ speaker being turned off.
1094 * So, we will swap the left and right mixer channels to compensate
1095 * for this.
1097 * XXX PR# 23620: The Dell C810 channels are not swapped. Match
1098 * on revision ID for now; this is probably wrong.
1100 if (revision == 0x10 && sc->type == ESS_MAESTRO3)
1101 sc->codec_flags = 0;
1102 else
1103 sc->codec_flags = AC97_HOST_SWAPPED_CHANNELS;
1106 /* Attach AC97 host interface */
1107 sc->host_if.arg = sc;
1108 sc->host_if.attach = esa_attach_codec;
1109 sc->host_if.read = esa_read_codec;
1110 sc->host_if.write = esa_write_codec;
1111 sc->host_if.reset = esa_reset_codec;
1112 sc->host_if.flags = esa_flags_codec;
1114 if (ac97_attach(&sc->host_if, self) != 0)
1115 return;
1117 /* initialize list management structures */
1118 sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0;
1119 sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS;
1120 sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0;
1121 sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS;
1122 sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0;
1123 sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS;
1124 sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC;
1125 sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC;
1127 /* initialize index maps */
1128 for (i = 0; i < ESA_NUM_VOICES * 2; i++) {
1129 sc->mixer_list.indexmap[i] = -1;
1130 sc->msrc_list.indexmap[i] = -1;
1131 sc->dma_list.indexmap[i] = -1;
1132 sc->adc1_list.indexmap[i] = -1;
1134 for (i = 0; i < ESA_NUM_VOICES; i++) {
1135 sc->voice[i].parent = sc->sc_dev;
1136 sc->voice[i].index = i;
1137 sc->sc_audiodev[i] =
1138 audio_attach_mi(&esa_hw_if, &sc->voice[i], sc->sc_dev);
1141 if (!pmf_device_register(self, esa_suspend, esa_resume))
1142 aprint_error_dev(self, "couldn't establish power handler\n");
1144 return;
1147 void
1148 esa_childdet(device_t self, device_t child)
1150 struct esa_softc *sc = device_private(self);
1151 int i;
1153 for (i = 0; i < ESA_NUM_VOICES; i++) {
1154 if (sc->sc_audiodev[i] == child) {
1155 sc->sc_audiodev[i] = NULL;
1156 break;
1159 KASSERT(i < ESA_NUM_VOICES);
1162 static int
1163 esa_detach(device_t self, int flags)
1165 struct esa_softc *sc;
1166 int i;
1168 sc = device_private(self);
1169 for (i = 0; i < ESA_NUM_VOICES; i++) {
1170 if (sc->sc_audiodev[i] != NULL)
1171 config_detach(sc->sc_audiodev[i], flags);
1174 if (sc->sc_ih != NULL)
1175 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1176 if (sc->sc_ios)
1177 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1179 free(sc->savemem, M_DEVBUF);
1181 return 0;
1184 static uint16_t
1185 esa_read_assp(struct esa_softc *sc, uint16_t region, uint16_t index)
1187 uint16_t data;
1188 bus_space_tag_t iot;
1189 bus_space_handle_t ioh;
1191 iot = sc->sc_iot;
1192 ioh = sc->sc_ioh;
1193 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1194 region & ESA_MEMTYPE_MASK);
1195 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1196 data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
1198 return data;
1201 static void
1202 esa_write_assp(struct esa_softc *sc, uint16_t region, uint16_t index,
1203 uint16_t data)
1205 bus_space_tag_t iot;
1206 bus_space_handle_t ioh;
1208 iot = sc->sc_iot;
1209 ioh = sc->sc_ioh;
1210 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1211 region & ESA_MEMTYPE_MASK);
1212 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1213 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
1215 return;
1218 static int
1219 esa_init_codec(struct esa_softc *sc)
1221 bus_space_tag_t iot;
1222 bus_space_handle_t ioh;
1223 uint32_t data;
1225 iot = sc->sc_iot;
1226 ioh = sc->sc_ioh;
1227 data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
1229 return (data & 0x1) ? 0 : 1;
1232 static int
1233 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1235 struct esa_softc *sc;
1237 sc = aux;
1238 sc->codec_if = codec_if;
1240 return 0;
1243 static int
1244 esa_read_codec(void *aux, uint8_t reg, uint16_t *result)
1246 struct esa_softc *sc;
1247 bus_space_tag_t iot;
1248 bus_space_handle_t ioh;
1250 sc = aux;
1251 iot = sc->sc_iot;
1252 ioh = sc->sc_ioh;
1253 if (esa_wait(sc))
1254 aprint_error_dev(sc->sc_dev, "esa_read_codec: timed out\n");
1255 bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
1256 delay(50);
1257 if (esa_wait(sc))
1258 aprint_error_dev(sc->sc_dev, "esa_read_codec: timed out\n");
1259 *result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
1261 return 0;
1264 static int
1265 esa_write_codec(void *aux, uint8_t reg, uint16_t data)
1267 struct esa_softc *sc;
1268 bus_space_tag_t iot;
1269 bus_space_handle_t ioh;
1271 sc = aux;
1272 iot = sc->sc_iot;
1273 ioh = sc->sc_ioh;
1274 if (esa_wait(sc)) {
1275 aprint_error_dev(sc->sc_dev, "esa_write_codec: timed out\n");
1276 return -1;
1278 bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
1279 bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
1280 delay(50);
1282 return 0;
1285 static int
1286 esa_reset_codec(void *aux)
1289 return 0;
1292 static enum ac97_host_flags
1293 esa_flags_codec(void *aux)
1295 struct esa_softc *sc;
1297 sc = aux;
1298 return sc->codec_flags;
1301 static int
1302 esa_wait(struct esa_softc *sc)
1304 int i, val;
1305 bus_space_tag_t iot;
1306 bus_space_handle_t ioh;
1308 iot = sc->sc_iot;
1309 ioh = sc->sc_ioh;
1310 for (i = 0; i < 20; i++) {
1311 val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
1312 if ((val & 1) == 0)
1313 return 0;
1314 delay(2);
1317 return -1;
1320 static int
1321 esa_init(struct esa_softc *sc)
1323 struct esa_voice *vc;
1324 bus_space_tag_t iot;
1325 bus_space_handle_t ioh;
1326 pcitag_t tag;
1327 pci_chipset_tag_t pc;
1328 uint32_t data, i, size;
1329 uint8_t reset_state;
1330 int data_bytes;
1332 iot = sc->sc_iot;
1333 ioh = sc->sc_ioh;
1334 tag = sc->sc_tag;
1335 pc = sc->sc_pct;
1336 data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
1337 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
1338 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255;
1340 /* Disable legacy emulation */
1341 data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
1342 data |= DISABLE_LEGACY;
1343 pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
1345 esa_config(sc);
1347 reset_state = esa_assp_halt(sc);
1349 esa_init_codec(sc);
1350 esa_codec_reset(sc);
1352 /* Zero kernel and mixer data */
1353 size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
1354 for (i = 0; i < size / 2; i++) {
1355 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1356 ESA_KDATA_BASE_ADDR + i, 0);
1357 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1358 ESA_KDATA_BASE_ADDR2 + i, 0);
1361 /* Init DMA pointer */
1362 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
1363 ESA_KDATA_DMA_XFER0);
1365 /* Write kernel code into memory */
1366 size = sizeof(esa_assp_kernel_image);
1367 for (i = 0; i < size / 2; i++)
1368 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1369 ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
1371 size = sizeof(esa_assp_minisrc_image);
1372 for (i = 0; i < size / 2; i++)
1373 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
1374 esa_assp_minisrc_image[i]);
1376 /* Write the coefficients for the low pass filter */
1377 size = sizeof(esa_minisrc_lpf_image);
1378 for (i = 0; i < size / 2; i++)
1379 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1380 0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
1381 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1382 0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
1383 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
1384 /* Init the mixer number */
1385 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1386 ESA_KDATA_MIXER_TASK_NUMBER, 0);
1387 /* Extreme kernel master volume */
1388 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1389 ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME);
1390 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1391 ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
1393 if (esa_amp_enable(sc))
1394 return -1;
1396 /* Zero entire DAC/ADC area */
1397 for (i = 0x1100; i < 0x1c00; i++)
1398 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
1400 /* set some sane defaults */
1401 for (i = 0; i < ESA_NUM_VOICES; i++) {
1402 vc = &sc->voice[i];
1403 vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i);
1404 vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2);
1407 esa_enable_interrupts(sc);
1409 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1410 reset_state | ESA_REGB_ENABLE_RESET);
1412 return 0;
1415 static void
1416 esa_config(struct esa_softc *sc)
1418 bus_space_tag_t iot;
1419 bus_space_handle_t ioh;
1420 pcitag_t tag;
1421 pci_chipset_tag_t pc;
1422 uint32_t data;
1424 iot = sc->sc_iot;
1425 ioh = sc->sc_ioh;
1426 tag = sc->sc_tag;
1427 pc = sc->sc_pct;
1429 data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1430 data &= ESA_REDUCED_DEBOUNCE;
1431 data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
1432 pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1434 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
1435 data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1436 data &= ~ESA_INT_CLK_SELECT;
1437 if (sc->type == ESS_MAESTRO3) {
1438 data &= ~ESA_INT_CLK_MULT_ENABLE;
1439 data |= ESA_INT_CLK_SRC_NOT_PCI;
1441 data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
1442 pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1444 if (sc->type == ESS_ALLEGRO1) {
1445 data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
1446 data |= ESA_IN_CLK_12MHZ_SELECT;
1447 pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
1450 data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
1451 data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
1452 data |= ESA_ASSP_CLK_49MHZ_SELECT; /* XXX: Assumes 49MHz DSP */
1453 data |= ESA_ASSP_0_WS_ENABLE;
1454 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
1456 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
1458 return;
1461 static uint8_t
1462 esa_assp_halt(struct esa_softc *sc)
1464 bus_space_tag_t iot;
1465 bus_space_handle_t ioh;
1466 uint8_t data, reset_state;
1468 iot = sc->sc_iot;
1469 ioh = sc->sc_ioh;
1470 data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
1471 reset_state = data & ~ESA_REGB_STOP_CLOCK;
1472 delay(10000);
1473 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1474 reset_state & ~ESA_REGB_ENABLE_RESET);
1475 delay(10000);
1477 return reset_state;
1480 static void
1481 esa_codec_reset(struct esa_softc *sc)
1483 bus_space_tag_t iot;
1484 bus_space_handle_t ioh;
1485 uint16_t data, dir;
1486 int retry;
1488 iot = sc->sc_iot;
1489 ioh = sc->sc_ioh;
1490 retry = 0;
1491 do {
1492 data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1493 dir = data | 0x10; /* assuming pci bus master? */
1495 /* remote codec config */
1496 data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
1497 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
1498 data & ~ESA_SECOND_CODEC_ID_MASK);
1499 data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
1500 bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
1501 data & ~ESA_COMMAND_ADDR_OUT);
1502 data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
1503 bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
1504 data & ~ESA_STATUS_ADDR_IN);
1506 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1507 ESA_IO_SRAM_ENABLE);
1508 delay(20);
1510 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1511 dir & ~ESA_GPO_PRIMARY_AC97);
1512 bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
1513 ~ESA_GPO_PRIMARY_AC97);
1514 bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
1515 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1516 dir | ESA_GPO_PRIMARY_AC97);
1517 delay(sc->delay1 * 1000);
1518 bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
1519 ESA_GPO_PRIMARY_AC97);
1520 delay(5);
1521 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1522 ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
1523 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1524 delay(sc->delay2 * 1000);
1526 esa_read_codec(sc, 0x7c, &data);
1527 if ((data == 0) || (data == 0xffff)) {
1528 retry++;
1529 if (retry > 3) {
1530 aprint_error_dev(sc->sc_dev,
1531 "esa_codec_reset: failed\n");
1532 break;
1534 aprint_normal_dev(sc->sc_dev,
1535 "esa_codec_reset: retrying\n");
1536 } else
1537 retry = 0;
1538 } while (retry);
1540 return;
1543 static int
1544 esa_amp_enable(struct esa_softc *sc)
1546 bus_space_tag_t iot;
1547 bus_space_handle_t ioh;
1548 uint32_t gpo, polarity_port, polarity;
1549 uint16_t data;
1551 iot = sc->sc_iot;
1552 ioh = sc->sc_ioh;
1553 switch (sc->type) {
1554 case ESS_ALLEGRO1:
1555 polarity_port = 0x1800;
1556 break;
1557 case ESS_MAESTRO3:
1558 polarity_port = 0x1100;
1559 break;
1560 default:
1561 aprint_error_dev(sc->sc_dev,
1562 "esa_amp_enable: Unknown chip type!!!\n");
1563 return 1;
1566 gpo = (polarity_port >> 8) & 0x0f;
1567 polarity = polarity_port >> 12;
1568 polarity = !polarity; /* Enable */
1569 polarity = polarity << gpo;
1570 gpo = 1 << gpo;
1571 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
1572 data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1573 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
1574 data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
1575 bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
1576 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1578 return 0;
1581 static void
1582 esa_enable_interrupts(struct esa_softc *sc)
1584 bus_space_tag_t iot;
1585 bus_space_handle_t ioh;
1586 uint8_t data;
1588 iot = sc->sc_iot;
1589 ioh = sc->sc_ioh;
1590 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
1591 ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
1592 data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
1593 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
1594 data | ESA_ASSP_HOST_INT_ENABLE);
1598 * List management
1600 static int
1601 esa_add_list(struct esa_voice *vc, struct esa_list *el,
1602 uint16_t val, int index)
1604 struct esa_softc *sc;
1606 sc = device_private(vc->parent);
1607 el->indexmap[index] = el->currlen;
1608 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1609 el->mem_addr + el->currlen,
1610 val);
1612 return el->currlen++;
1615 static void
1616 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index)
1618 struct esa_softc *sc;
1619 uint16_t val;
1620 int lastindex;
1621 int vindex;
1622 int i;
1624 sc = device_private(vc->parent);
1625 lastindex = el->currlen - 1;
1626 vindex = el->indexmap[index];
1628 /* reset our virtual index */
1629 el->indexmap[index] = -1;
1631 if (vindex != lastindex) {
1632 val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1633 el->mem_addr + lastindex);
1634 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1635 el->mem_addr + vindex,
1636 val);
1637 for (i = 0; i < ESA_NUM_VOICES * 2; i++)
1638 if (el->indexmap[i] == lastindex)
1639 break;
1640 if (i >= ESA_NUM_VOICES * 2)
1641 aprint_error_dev(sc->sc_dev,
1642 "esa_remove_list: invalid task index\n");
1643 else
1644 el->indexmap[i] = vindex;
1647 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1648 el->mem_addr + lastindex, 0);
1649 el->currlen--;
1651 return;
1654 static bool
1655 esa_suspend(device_t dv, pmf_qual_t qual)
1657 struct esa_softc *sc = device_private(dv);
1658 bus_space_tag_t iot = sc->sc_iot;
1659 bus_space_handle_t ioh = sc->sc_ioh;
1660 int i, index;
1662 index = 0;
1664 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
1665 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
1667 esa_assp_halt(sc);
1669 /* Save ASSP state */
1670 for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1671 i++)
1672 sc->savemem[index++] = esa_read_assp(sc,
1673 ESA_MEMTYPE_INTERNAL_CODE, i);
1674 for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1675 i++)
1676 sc->savemem[index++] = esa_read_assp(sc,
1677 ESA_MEMTYPE_INTERNAL_DATA, i);
1679 return true;
1682 static bool
1683 esa_resume(device_t dv, pmf_qual_t qual)
1685 struct esa_softc *sc = device_private(dv);
1686 bus_space_tag_t iot = sc->sc_iot;
1687 bus_space_handle_t ioh = sc->sc_ioh;
1688 int i, index;
1689 uint8_t reset_state;
1690 pcireg_t data;
1692 index = 0;
1694 delay(10000);
1696 data = pci_conf_read(sc->sc_pct, sc->sc_tag, PCI_LEGACY_AUDIO_CTRL);
1697 pci_conf_write(sc->sc_pct, sc->sc_tag, PCI_LEGACY_AUDIO_CTRL,
1698 data | DISABLE_LEGACY);
1700 bus_space_write_4(iot, ioh, ESA_PCI_ACPI_CONTROL, ESA_PCI_ACPI_D0);
1702 esa_config(sc);
1704 reset_state = esa_assp_halt(sc);
1706 esa_init_codec(sc);
1707 esa_codec_reset(sc);
1709 /* restore ASSP */
1710 for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1711 i++)
1712 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
1713 sc->savemem[index++]);
1714 for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1715 i++)
1716 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
1717 sc->savemem[index++]);
1719 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
1720 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1721 reset_state | ESA_REGB_ENABLE_RESET);
1723 esa_enable_interrupts(sc);
1724 esa_amp_enable(sc);
1726 /* Finally, power up AC97 codec */
1727 delay(1000);
1728 sc->codec_if->vtbl->restore_ports(sc->codec_if);
1730 return true;
1733 static uint32_t
1734 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
1736 uint16_t hi, lo;
1737 uint32_t addr;
1738 int data_offset;
1740 data_offset = ch->data_offset;
1741 hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1742 ESA_CDATA_HOST_SRC_CURRENTH);
1743 lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1744 ESA_CDATA_HOST_SRC_CURRENTL);
1746 addr = lo | ((uint32_t)hi << 16);
1747 return (addr - ch->start);
1750 static paddr_t
1751 esa_mappage(void *addr, void *mem, off_t off, int prot)
1753 struct esa_voice *vc;
1754 struct esa_softc *sc;
1755 struct esa_dma *p;
1757 vc = addr;
1758 sc = device_private(vc->parent);
1759 if (off < 0)
1760 return -1;
1761 for (p = vc->dma; p && KERNADDR(p) != mem; p = p->next)
1762 continue;
1763 if (p == NULL)
1764 return -1;
1765 return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
1766 off, prot, BUS_DMA_WAITOK);