1 /* $NetBSD: esa.c,v 1.52 2009/11/26 15:17:09 njoly Exp $ */
4 * Copyright (c) 2001-2008 Jared D. McNeill <jmcneill@invisible.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
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>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
53 #include <sys/select.h>
54 #include <sys/audioio.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>
74 #define ESA_DAC_DATA 0x1100
81 static const struct esa_card_type
{
82 uint16_t pci_vendor_id
;
83 uint16_t pci_product_id
;
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 },
96 static struct audio_device esa_device
= {
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
*,
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,
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,
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
*,
162 static void esa_remove_list(struct esa_voice
*, struct esa_list
*,
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
= {
207 NULL
, /* init_output */
208 NULL
, /* init_input */
209 NULL
, /* start_output */
210 NULL
, /* start_input */
213 NULL
, /* speaker_ctl */
221 esa_round_buffersize
,
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
);
238 esa_query_encoding(void *hdl
, struct audio_encoding
*ae
)
241 if (ae
->index
< 0 || ae
->index
>= ESA_NENCODINGS
)
243 *ae
= esa_encoding
[ae
->index
];
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
;
260 for (mode
= AUMODE_RECORD
; mode
!= -1;
261 mode
= (mode
== AUMODE_RECORD
) ? AUMODE_PLAY
: -1) {
262 if ((setmode
& mode
) == 0)
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))
286 i
= auconv_set_converter(esa_formats
, ESA_NFORMATS
,
287 mode
, p
, FALSE
, fil
);
290 if (fil
->req_size
> 0)
291 p
= &fil
->filters
[0].param
;
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
;
310 sc
= device_private(vc
->parent
);
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)
318 vc
->play
.data_offset
= ESA_DAC_DATA
+ (data_bytes
* vc
->index
);
319 if (p
->channels
== 1)
323 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
324 vc
->play
.data_offset
+ ESA_SRC3_MODE_OFFSET
,
326 if (p
->precision
== 8)
330 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
331 vc
->play
.data_offset
+ ESA_SRC3_WORD_LENGTH_OFFSET
,
333 if ((freq
= ((p
->sample_rate
<< 15) + 24000) / 48000) != 0) {
336 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
337 vc
->play
.data_offset
+ ESA_CDATA_FREQUENCY
, freq
);
340 vc
->rec
.data_offset
= ESA_DAC_DATA
+ (data_bytes
* vc
->index
) +
342 if (r
->channels
== 1)
346 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
347 vc
->rec
.data_offset
+ ESA_SRC3_MODE_OFFSET
,
349 if (r
->precision
== 8)
353 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
354 vc
->rec
.data_offset
+ ESA_SRC3_WORD_LENGTH_OFFSET
,
356 if ((freq
= ((r
->sample_rate
<< 15) + 24000) / 48000) != 0) {
359 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
360 vc
->rec
.data_offset
+ ESA_CDATA_FREQUENCY
, freq
);
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 */
374 esa_halt_output(void *hdl
)
376 struct esa_voice
*vc
;
377 struct esa_softc
*sc
;
379 bus_space_handle_t ioh
;
383 sc
= device_private(vc
->parent
);
386 if (vc
->play
.active
== 0)
391 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
392 ESA_CDATA_INSTANCE_READY
+ vc
->play
.data_offset
, 0);
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
);
417 esa_halt_input(void *hdl
)
419 struct esa_voice
*vc
;
420 struct esa_softc
*sc
;
422 bus_space_handle_t ioh
;
426 sc
= device_private(vc
->parent
);
429 if (vc
->rec
.active
== 0)
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
);
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
;
467 p
= malloc(sizeof(*p
), type
, flags
);
471 sc
= device_private(vc
->parent
);
472 error
= esa_allocmem(sc
, size
, 16, p
);
475 aprint_error_dev(sc
->sc_dev
,
476 "%s: not enough memory\n", __func__
);
486 esa_free(void *hdl
, void *addr
, struct malloc_type
*type
)
488 struct esa_voice
*vc
;
489 struct esa_softc
*sc
;
494 sc
= device_private(vc
->parent
);
495 for (pp
= &vc
->dma
; (p
= *pp
) != NULL
; pp
= &p
->next
)
496 if (KERNADDR(p
) == addr
) {
505 esa_getdev(void *hdl
, struct audio_device
*ret
)
513 esa_set_port(void *hdl
, mixer_ctrl_t
*mc
)
515 struct esa_voice
*vc
;
516 struct esa_softc
*sc
;
519 sc
= device_private(vc
->parent
);
520 return sc
->codec_if
->vtbl
->mixer_set_port(sc
->codec_if
, mc
);
524 esa_get_port(void *hdl
, mixer_ctrl_t
*mc
)
526 struct esa_voice
*vc
;
527 struct esa_softc
*sc
;
530 sc
= device_private(vc
->parent
);
531 return sc
->codec_if
->vtbl
->mixer_get_port(sc
->codec_if
, mc
);
535 esa_query_devinfo(void *hdl
, mixer_devinfo_t
*di
)
537 struct esa_voice
*vc
;
538 struct esa_softc
*sc
;
541 sc
= device_private(vc
->parent
);
542 return sc
->codec_if
->vtbl
->query_devinfo(sc
->codec_if
, di
);
546 esa_round_buffersize(void *hdl
, int direction
, size_t bufsize
)
553 esa_get_props(void *hdl
)
556 return AUDIO_PROP_MMAP
| AUDIO_PROP_INDEPENDENT
| AUDIO_PROP_FULLDUPLEX
;
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
;
567 bus_space_handle_t ioh
;
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
;
574 sc
= device_private(vc
->parent
);
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;
589 for (p
= vc
->dma
; p
&& KERNADDR(p
) != start
; p
= p
->next
)
592 aprint_error_dev(sc
->sc_dev
, "%s: bad addr %p\n", __func__
,
598 vc
->play
.intr
= intr
;
599 vc
->play
.arg
= intrarg
;
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
));
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
,
665 esa_add_list(vc
, &sc
->dma_list
, dac_data
>> ESA_DP_SHIFT_COUNT
,
667 esa_add_list(vc
, &sc
->mixer_list
, dac_data
>> ESA_DP_SHIFT_COUNT
,
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
]);
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
;
701 bus_space_handle_t ioh
;
702 uint32_t data
, bufaddr
, i
;
704 int data_bytes
, adc_data
, dsp_in_size
;
705 int dsp_out_size
, dsp_in_buf
, dsp_out_buf
;
708 sc
= device_private(vc
->parent
);
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 */
729 for (p
= vc
->dma
; p
&& KERNADDR(p
) != start
; p
= p
->next
)
732 aprint_error_dev(sc
->sc_dev
, "%s: bad addr %p\n",
739 vc
->rec
.arg
= intrarg
;
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
));
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
);
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
,
825 /* Interrupt handler */
829 struct esa_softc
*sc
;
830 struct esa_voice
*vc
;
832 bus_space_handle_t ioh
;
843 status
= bus_space_read_1(iot
, ioh
, ESA_HOST_INT_STATUS
);
847 /* ack the interrupt */
848 bus_space_write_1(iot
, ioh
, ESA_HOST_INT_STATUS
, status
);
850 if (status
& ESA_HV_INT_PENDING
) {
853 aprint_normal_dev(sc
->sc_dev
, "hardware volume interrupt\n");
854 event
= bus_space_read_1(iot
, ioh
, ESA_HW_VOL_COUNTER_MASTER
);
856 case 0xaa: /* volume up */
857 pmf_event_inject(NULL
, PMFE_AUDIO_VOLUME_UP
);
859 case 0x66: /* volume down */
860 pmf_event_inject(NULL
, PMFE_AUDIO_VOLUME_DOWN
);
862 case 0x88: /* mute */
863 pmf_event_inject(NULL
, PMFE_AUDIO_VOLUME_TOGGLE
);
866 aprint_normal_dev(sc
->sc_dev
,
867 "unknown hwvol event 0x%02x\n", event
);
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)
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
++) {
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
) %
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
) %
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
);
921 esa_allocmem(struct esa_softc
*sc
, size_t size
, size_t align
,
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
);
933 error
= bus_dmamem_map(sc
->sc_dmat
, p
->segs
, p
->nsegs
, p
->size
,
934 &p
->addr
, BUS_DMA_NOWAIT
| BUS_DMA_COHERENT
);
938 error
= bus_dmamap_create(sc
->sc_dmat
, p
->size
, 1, p
->size
, 0,
939 BUS_DMA_NOWAIT
, &p
->map
);
943 error
= bus_dmamap_load(sc
->sc_dmat
, p
->map
, p
->addr
, p
->size
, NULL
,
951 bus_dmamap_destroy(sc
->sc_dmat
, p
->map
);
953 bus_dmamem_unmap(sc
->sc_dmat
, p
->addr
, p
->size
);
955 bus_dmamem_free(sc
->sc_dmat
, p
->segs
, p
->nsegs
);
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
);
973 * Supporting Subroutines
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
:
996 esa_attach(device_t parent
, device_t self
, void *aux
)
998 struct esa_softc
*sc
;
999 struct pci_attach_args
*pa
;
1001 pci_chipset_tag_t pc
;
1002 pci_intr_handle_t ih
;
1003 const struct esa_card_type
*card
;
1004 const char *intrstr
;
1011 sc
= device_private(self
);
1012 pa
= (struct pci_attach_args
*)aux
;
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
;
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");
1042 /* Initialize softc */
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");
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
);
1062 aprint_normal_dev(sc
->sc_dev
, "interrupting at %s\n", intrstr
);
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
);
1072 if (esa_init(sc
) == -1) {
1073 aprint_error_dev(sc
->sc_dev
,
1074 "esa_attach: unable to initialize the card\n");
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");
1089 * Every card I've seen has had their channels swapped with respect
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
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;
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)
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");
1148 esa_childdet(device_t self
, device_t child
)
1150 struct esa_softc
*sc
= device_private(self
);
1153 for (i
= 0; i
< ESA_NUM_VOICES
; i
++) {
1154 if (sc
->sc_audiodev
[i
] == child
) {
1155 sc
->sc_audiodev
[i
] = NULL
;
1159 KASSERT(i
< ESA_NUM_VOICES
);
1163 esa_detach(device_t self
, int flags
)
1165 struct esa_softc
*sc
;
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
);
1177 bus_space_unmap(sc
->sc_iot
, sc
->sc_ioh
, sc
->sc_ios
);
1179 free(sc
->savemem
, M_DEVBUF
);
1185 esa_read_assp(struct esa_softc
*sc
, uint16_t region
, uint16_t index
)
1188 bus_space_tag_t iot
;
1189 bus_space_handle_t 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
);
1202 esa_write_assp(struct esa_softc
*sc
, uint16_t region
, uint16_t index
,
1205 bus_space_tag_t iot
;
1206 bus_space_handle_t 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
);
1219 esa_init_codec(struct esa_softc
*sc
)
1221 bus_space_tag_t iot
;
1222 bus_space_handle_t ioh
;
1227 data
= bus_space_read_1(iot
, ioh
, ESA_CODEC_COMMAND
);
1229 return (data
& 0x1) ? 0 : 1;
1233 esa_attach_codec(void *aux
, struct ac97_codec_if
*codec_if
)
1235 struct esa_softc
*sc
;
1238 sc
->codec_if
= codec_if
;
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
;
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);
1258 aprint_error_dev(sc
->sc_dev
, "esa_read_codec: timed out\n");
1259 *result
= bus_space_read_2(iot
, ioh
, ESA_CODEC_DATA
);
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
;
1275 aprint_error_dev(sc
->sc_dev
, "esa_write_codec: timed out\n");
1278 bus_space_write_2(iot
, ioh
, ESA_CODEC_DATA
, data
);
1279 bus_space_write_1(iot
, ioh
, ESA_CODEC_COMMAND
, reg
& 0x7f);
1286 esa_reset_codec(void *aux
)
1292 static enum ac97_host_flags
1293 esa_flags_codec(void *aux
)
1295 struct esa_softc
*sc
;
1298 return sc
->codec_flags
;
1302 esa_wait(struct esa_softc
*sc
)
1305 bus_space_tag_t iot
;
1306 bus_space_handle_t ioh
;
1310 for (i
= 0; i
< 20; i
++) {
1311 val
= bus_space_read_1(iot
, ioh
, ESA_CODEC_STATUS
);
1321 esa_init(struct esa_softc
*sc
)
1323 struct esa_voice
*vc
;
1324 bus_space_tag_t iot
;
1325 bus_space_handle_t ioh
;
1327 pci_chipset_tag_t pc
;
1328 uint32_t data
, i
, size
;
1329 uint8_t reset_state
;
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
);
1347 reset_state
= esa_assp_halt(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
))
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
++) {
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
);
1416 esa_config(struct esa_softc
*sc
)
1418 bus_space_tag_t iot
;
1419 bus_space_handle_t ioh
;
1421 pci_chipset_tag_t pc
;
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
);
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
;
1470 data
= bus_space_read_1(iot
, ioh
, ESA_DSP_PORT_CONTROL_REG_B
);
1471 reset_state
= data
& ~ESA_REGB_STOP_CLOCK
;
1473 bus_space_write_1(iot
, ioh
, ESA_DSP_PORT_CONTROL_REG_B
,
1474 reset_state
& ~ESA_REGB_ENABLE_RESET
);
1481 esa_codec_reset(struct esa_softc
*sc
)
1483 bus_space_tag_t iot
;
1484 bus_space_handle_t ioh
;
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
);
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
);
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)) {
1530 aprint_error_dev(sc
->sc_dev
,
1531 "esa_codec_reset: failed\n");
1534 aprint_normal_dev(sc
->sc_dev
,
1535 "esa_codec_reset: retrying\n");
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
;
1555 polarity_port
= 0x1800;
1558 polarity_port
= 0x1100;
1561 aprint_error_dev(sc
->sc_dev
,
1562 "esa_amp_enable: Unknown chip type!!!\n");
1566 gpo
= (polarity_port
>> 8) & 0x0f;
1567 polarity
= polarity_port
>> 12;
1568 polarity
= !polarity
; /* Enable */
1569 polarity
= polarity
<< 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);
1582 esa_enable_interrupts(struct esa_softc
*sc
)
1584 bus_space_tag_t iot
;
1585 bus_space_handle_t 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
);
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
,
1612 return el
->currlen
++;
1616 esa_remove_list(struct esa_voice
*vc
, struct esa_list
*el
, int index
)
1618 struct esa_softc
*sc
;
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
,
1637 for (i
= 0; i
< ESA_NUM_VOICES
* 2; i
++)
1638 if (el
->indexmap
[i
] == lastindex
)
1640 if (i
>= ESA_NUM_VOICES
* 2)
1641 aprint_error_dev(sc
->sc_dev
,
1642 "esa_remove_list: invalid task index\n");
1644 el
->indexmap
[i
] = vindex
;
1647 esa_write_assp(sc
, ESA_MEMTYPE_INTERNAL_DATA
,
1648 el
->mem_addr
+ lastindex
, 0);
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
;
1664 bus_space_write_2(iot
, ioh
, ESA_HOST_INT_CTRL
, 0);
1665 bus_space_write_1(iot
, ioh
, ESA_ASSP_CONTROL_C
, 0);
1669 /* Save ASSP state */
1670 for (i
= ESA_REV_B_CODE_MEMORY_BEGIN
; i
<= ESA_REV_B_CODE_MEMORY_END
;
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
;
1676 sc
->savemem
[index
++] = esa_read_assp(sc
,
1677 ESA_MEMTYPE_INTERNAL_DATA
, i
);
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
;
1689 uint8_t reset_state
;
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
);
1704 reset_state
= esa_assp_halt(sc
);
1707 esa_codec_reset(sc
);
1710 for (i
= ESA_REV_B_CODE_MEMORY_BEGIN
; i
<= ESA_REV_B_CODE_MEMORY_END
;
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
;
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
);
1726 /* Finally, power up AC97 codec */
1728 sc
->codec_if
->vtbl
->restore_ports(sc
->codec_if
);
1734 esa_get_pointer(struct esa_softc
*sc
, struct esa_channel
*ch
)
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
);
1751 esa_mappage(void *addr
, void *mem
, off_t off
, int prot
)
1753 struct esa_voice
*vc
;
1754 struct esa_softc
*sc
;
1758 sc
= device_private(vc
->parent
);
1761 for (p
= vc
->dma
; p
&& KERNADDR(p
) != mem
; p
= p
->next
)
1765 return bus_dmamem_mmap(sc
->sc_dmat
, p
->segs
, p
->nsegs
,
1766 off
, prot
, BUS_DMA_WAITOK
);