xref: /openbsd/sys/dev/pci/esa.c (revision d25d28bf)
1 /*	$OpenBSD: esa.c,v 1.31 2016/09/19 06:46:44 ratchov Exp $	*/
2 /* $NetBSD: esa.c,v 1.12 2002/03/24 14:17:35 jmcneill Exp $ */
3 
4 /*
5  * Copyright (c) 2001, 2002 Jared D. McNeill <jmcneill@invisible.ca>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Shamelessly stolen from NetBSD who based it on FreeBSD's who in turn
31  * based it on Linux's driver.  What a wonderful world.
32  *
33  *
34  * ESS Allegro-1 / Maestro3 Audio Driver
35  *
36  * Based on the FreeBSD maestro3 driver and the NetBSD eap driver.
37  * Original driver by Don Kim.
38  *
39  * The list management code could possibly be written better, but what
40  * we have right now does the job nicely. Thanks to Zach Brown <zab@zabbo.net>
41  * and Andrew MacDonald <amac@epsilon.yi.org> for helping me debug the
42  * problems with the original list management code present in the Linux
43  * driver.
44  */
45 
46 #include <sys/types.h>
47 #include <sys/errno.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/device.h>
52 #include <sys/conf.h>
53 #include <sys/exec.h>
54 #include <sys/selinfo.h>
55 #include <sys/audioio.h>
56 
57 #include <machine/bus.h>
58 #include <machine/intr.h>
59 
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pcivar.h>
62 
63 #include <dev/audio_if.h>
64 #include <dev/ic/ac97.h>
65 
66 #include <dev/pci/esareg.h>
67 #include <dev/pci/esavar.h>
68 #include <dev/microcode/esa/esadsp.h>
69 
70 #define PCI_CBIO	0x10
71 
72 #define ESA_DAC_DATA	0x1100
73 
74 enum {
75 	ESS_ALLEGRO1,
76 	ESS_MAESTRO3
77 };
78 
79 static struct esa_card_type {
80 	u_int16_t pci_vendor_id;
81 	u_int16_t pci_product_id;
82 	int type;
83 	int delay1, delay2;
84 } esa_card_types[] = {
85 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989,
86 	  ESS_ALLEGRO1, 50, 800 },
87 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3,
88 	  ESS_MAESTRO3, 20, 500 },
89 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2,
90 	  ESS_MAESTRO3, 20, 500 },
91 	{ 0, 0, 0, 0, 0 }
92 };
93 
94 int		esa_match(struct device *, void *, void *);
95 void		esa_attach(struct device *, struct device *, void *);
96 int		esa_detach(struct device *, int);
97 int		esa_activate(struct device *, int);
98 
99 /* audio(9) functions */
100 int		esa_open(void *, int);
101 void		esa_close(void *);
102 int		esa_set_params(void *, int, int, struct audio_params *,
103 			       struct audio_params *);
104 int		esa_round_blocksize(void *, int);
105 int		esa_commit_settings(void *);
106 int		esa_halt_output(void *);
107 int		esa_halt_input(void *);
108 int		esa_set_port(void *, mixer_ctrl_t *);
109 int		esa_get_port(void *, mixer_ctrl_t *);
110 int		esa_query_devinfo(void *, mixer_devinfo_t *);
111 void *		esa_malloc(void *, int, size_t, int, int);
112 void		esa_free(void *, void *, int);
113 size_t		esa_round_buffersize(void *, int, size_t);
114 int		esa_get_props(void *);
115 int		esa_trigger_output(void *, void *, void *, int,
116 				   void (*)(void *), void *,
117 				   struct audio_params *);
118 int		esa_trigger_input(void *, void *, void *, int,
119 				  void (*)(void *), void *,
120 				  struct audio_params *);
121 
122 int		esa_intr(void *);
123 int		esa_allocmem(struct esa_softc *, size_t, size_t,
124 			     struct esa_dma *);
125 int		esa_freemem(struct esa_softc *, struct esa_dma *);
126 
127 /* Supporting subroutines */
128 u_int16_t	esa_read_assp(struct esa_softc *, u_int16_t, u_int16_t);
129 void		esa_write_assp(struct esa_softc *, u_int16_t, u_int16_t,
130 			       u_int16_t);
131 int		esa_init_codec(struct esa_softc *);
132 int		esa_attach_codec(void *, struct ac97_codec_if *);
133 int		esa_read_codec(void *, u_int8_t, u_int16_t *);
134 int		esa_write_codec(void *, u_int8_t, u_int16_t);
135 void		esa_reset_codec(void *);
136 enum ac97_host_flags	esa_flags_codec(void *);
137 int		esa_wait(struct esa_softc *);
138 int		esa_init(struct esa_softc *);
139 void		esa_config(struct esa_softc *);
140 u_int8_t	esa_assp_halt(struct esa_softc *);
141 void		esa_codec_reset(struct esa_softc *);
142 int		esa_amp_enable(struct esa_softc *);
143 void		esa_enable_interrupts(struct esa_softc *);
144 u_int32_t	esa_get_pointer(struct esa_softc *, struct esa_channel *);
145 
146 /* list management */
147 int		esa_add_list(struct esa_voice *, struct esa_list *, u_int16_t,
148 			     int);
149 void		esa_remove_list(struct esa_voice *, struct esa_list *, int);
150 
151 /* power management */
152 int		esa_suspend(struct esa_softc *);
153 int		esa_resume(struct esa_softc *);
154 
155 struct audio_hw_if esa_hw_if = {
156 	esa_open,
157 	esa_close,
158 	esa_set_params,
159 	esa_round_blocksize,
160 	esa_commit_settings,
161 	NULL,			/* init_output */
162 	NULL,			/* init_input */
163 	NULL,			/* start_output */
164 	NULL,			/* start_input */
165 	esa_halt_output,
166 	esa_halt_input,
167 	NULL,			/* speaker_ctl */
168 	NULL,			/* getfd */
169 	esa_set_port,
170 	esa_get_port,
171 	esa_query_devinfo,
172 	esa_malloc,
173 	esa_free,
174 	esa_round_buffersize,
175 	esa_get_props,
176 	esa_trigger_output,
177 	esa_trigger_input
178 };
179 
180 struct cfdriver esa_cd = {
181 	NULL, "esa", DV_DULL
182 };
183 
184 struct cfattach esa_ca = {
185 	sizeof(struct esa_softc), esa_match, esa_attach,
186 	esa_detach, esa_activate
187 };
188 
189 /*
190  * audio(9) functions
191  */
192 
193 int
194 esa_open(void *hdl, int flags)
195 {
196 
197 	return (0);
198 }
199 
200 void
201 esa_close(void *hdl)
202 {
203 
204 	return;
205 }
206 
207 int
208 esa_set_params(void *hdl, int setmode, int usemode, struct audio_params *play,
209 	       struct audio_params *rec)
210 {
211 	struct esa_voice *vc = hdl;
212 	//struct esa_softc *sc = (struct esa_softc *)vc->parent;
213 	struct esa_channel *ch;
214 	struct audio_params *p;
215 	int mode;
216 
217 	for (mode = AUMODE_RECORD; mode != -1;
218 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
219 		if ((setmode & mode) == 0)
220 			continue;
221 
222 		switch (mode) {
223 		case AUMODE_PLAY:
224 			p = play;
225 			ch = &vc->play;
226 			break;
227 		case AUMODE_RECORD:
228 			p = rec;
229 			ch = &vc->rec;
230 			break;
231 		}
232 
233 		if (p->sample_rate < ESA_MINRATE)
234 			p->sample_rate = ESA_MINRATE;
235 		if (p->sample_rate > ESA_MAXRATE)
236 			p->sample_rate = ESA_MAXRATE;
237 		if (p->precision > 16)
238 			p->precision = 16;
239 		if (p->channels > 2)
240 			p->channels = 2;
241 
242 		switch(p->encoding) {
243 		case AUDIO_ENCODING_SLINEAR_LE:
244 			if (p->precision != 16)
245 				return EINVAL;
246 			break;
247 		case AUDIO_ENCODING_ULINEAR_LE:
248 		case AUDIO_ENCODING_ULINEAR_BE:
249 			if (p->precision != 8)
250 				return EINVAL;
251 			break;
252 		default:
253 			return (EINVAL);
254 		}
255 		p->bps = AUDIO_BPS(p->precision);
256 		p->msb = 1;
257 
258 		ch->mode = *p;
259 	}
260 
261 	return (0);
262 }
263 
264 int
265 esa_commit_settings(void *hdl)
266 {
267 	struct esa_voice *vc = hdl;
268 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
269 	struct audio_params *p = &vc->play.mode;
270 	struct audio_params *r = &vc->rec.mode;
271 	u_int32_t data;
272 	u_int32_t freq;
273 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
274 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
275 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
276 			   &~ 255;
277 
278 	/* playback */
279 	vc->play.data_offset = ESA_DAC_DATA + (data_bytes * vc->index);
280 	if (p->channels == 1)
281 		data = 1;
282 	else
283 		data = 0;
284 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
285 		       vc->play.data_offset + ESA_SRC3_MODE_OFFSET,
286 		       data);
287 	if (p->precision == 8)
288 		data = 1;
289 	else
290 		data = 0;
291 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
292 		       vc->play.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
293 		       data);
294 	if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) {
295 		freq--;
296 	}
297 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
298 		       vc->play.data_offset + ESA_CDATA_FREQUENCY, freq);
299 
300 	/* recording */
301 	vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * vc->index) +
302 			      (data_bytes / 2);
303 	if (r->channels == 1)
304 		data = 1;
305 	else
306 		data = 0;
307 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
308 		       vc->rec.data_offset + ESA_SRC3_MODE_OFFSET,
309 		       data);
310 	if (r->precision == 8)
311 		data = 1;
312 	else
313 		data = 0;
314 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
315 		       vc->rec.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
316 		       data);
317 	if ((freq = ((r->sample_rate << 15) + 24000) / 48000) != 0) {
318 		freq--;
319 	}
320 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
321 		       vc->rec.data_offset + ESA_CDATA_FREQUENCY, freq);
322 
323 	return (0);
324 };
325 
326 int
327 esa_round_blocksize(void *hdl, int bs)
328 {
329 	struct esa_voice *vc = hdl;
330 
331 	/*
332 	 * Surely there has to be a better solution...
333 	 */
334 	vc->play.blksize = vc->rec.blksize = 4096;
335 
336 	return (vc->play.blksize);
337 }
338 
339 int
340 esa_halt_output(void *hdl)
341 {
342 	struct esa_voice *vc = hdl;
343 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
344 	bus_space_tag_t iot = sc->sc_iot;
345 	bus_space_handle_t ioh = sc->sc_ioh;
346 	u_int16_t data;
347 
348 	if (vc->play.active == 0)
349 		return (0);
350 
351 	mtx_enter(&audio_lock);
352 	vc->play.active = 0;
353 
354 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
355 		       ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0);
356 
357 	sc->sc_ntimers--;
358 	if (sc->sc_ntimers == 0) {
359 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
360 			       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
361 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
362 			       ESA_KDATA_TIMER_COUNT_CURRENT, 0);
363 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
364 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
365 		    data & ~ESA_CLKRUN_GEN_ENABLE);
366 	}
367 
368 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
369 		       ESA_KDATA_MIXER_TASK_NUMBER,
370 		       sc->mixer_list.indexmap[vc->index]);
371 	/* remove ourselves from the packed lists */
372 	esa_remove_list(vc, &sc->mixer_list, vc->index);
373 	esa_remove_list(vc, &sc->dma_list, vc->index);
374 	esa_remove_list(vc, &sc->msrc_list, vc->index);
375 	mtx_leave(&audio_lock);
376 	return (0);
377 }
378 
379 int
380 esa_halt_input(void *hdl)
381 {
382 	struct esa_voice *vc = hdl;
383 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
384 	bus_space_tag_t iot = sc->sc_iot;
385 	bus_space_handle_t ioh = sc->sc_ioh;
386 	u_int32_t data;
387 
388 	if (vc->rec.active == 0)
389 		return (0);
390 
391 	mtx_enter(&audio_lock);
392 	vc->rec.active = 0;
393 
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);
403 	}
404 
405 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset +
406 		       ESA_CDATA_INSTANCE_READY, 0);
407 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
408 		       0);
409 
410 	/* remove ourselves from the packed lists */
411 	esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES);
412 	esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES);
413 	esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES);
414 	mtx_leave(&audio_lock);
415 	return (0);
416 }
417 
418 void *
419 esa_malloc(void *hdl, int direction, size_t size, int type, int flags)
420 {
421 	struct esa_voice *vc = hdl;
422 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
423 	struct esa_dma *p;
424 	int error;
425 
426 	p = malloc(sizeof(*p), type, flags);
427 	if (!p)
428 		return (0);
429 	error = esa_allocmem(sc, size, 16, p);
430 	if (error) {
431 		free(p, type, 0);
432 		printf("%s: esa_malloc: not enough memory\n",
433 		    sc->sc_dev.dv_xname);
434 		return (0);
435 	}
436 	p->next = vc->dma;
437 	vc->dma = p;
438 
439 	return (KERNADDR(p));
440 }
441 
442 void
443 esa_free(void *hdl, void *addr, int type)
444 {
445 	struct esa_voice *vc = hdl;
446 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
447 	struct esa_dma *p;
448 	struct esa_dma **pp;
449 
450 	for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next)
451 		if (KERNADDR(p) == addr) {
452 			esa_freemem(sc, p);
453 			*pp = p->next;
454 			free(p, type, 0);
455 			return;
456 		}
457 }
458 
459 int
460 esa_set_port(void *hdl, mixer_ctrl_t *mc)
461 {
462 	struct esa_voice *vc = hdl;
463 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
464 
465 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc));
466 }
467 
468 int
469 esa_get_port(void *hdl, mixer_ctrl_t *mc)
470 {
471 	struct esa_voice *vc = hdl;
472 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
473 
474 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc));
475 }
476 
477 int
478 esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
479 {
480 	struct esa_voice *vc = hdl;
481 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
482 
483 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di));
484 }
485 
486 size_t
487 esa_round_buffersize(void *hdl, int direction, size_t bufsize)
488 {
489 	struct esa_voice *vc = hdl;
490 
491 	/*
492 	 * We must be able to do better than this...
493 	 */
494 	vc->play.bufsize = vc->rec.bufsize = 65536;
495 
496 	return (vc->play.bufsize);
497 }
498 
499 int
500 esa_get_props(void *hdl)
501 {
502 
503 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
504 }
505 
506 int
507 esa_trigger_output(void *hdl, void *start, void *end, int blksize,
508 			void (*intr)(void *), void *intrarg,
509 			struct audio_params *param)
510 {
511 	struct esa_voice *vc = hdl;
512 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
513 	struct esa_dma *p;
514 	bus_space_tag_t iot = sc->sc_iot;
515 	bus_space_handle_t ioh = sc->sc_ioh;
516 	u_int32_t data;
517 	u_int32_t bufaddr;
518 	u_int32_t i;
519 	size_t size;
520 
521 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
522 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
523 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
524 			   &~ 255;
525 	int dac_data = ESA_DAC_DATA + (data_bytes * vc->index);
526 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
527 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
528 	int dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
529 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
530 
531 	if (vc->play.active)
532 		return (EINVAL);
533 
534 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
535 		;
536 	if (!p) {
537 		printf("%s: esa_trigger_output: bad addr %p\n",
538 		    sc->sc_dev.dv_xname, start);
539 		return (EINVAL);
540 	}
541 
542 	vc->play.active = 1;
543 	vc->play.intr = intr;
544 	vc->play.arg = intrarg;
545 	vc->play.pos = 0;
546 	vc->play.count = 0;
547 	vc->play.buf = start;
548 	size = (size_t)(((caddr_t)end - (caddr_t)start));
549 	bufaddr = DMAADDR(p);
550 	vc->play.start = bufaddr;
551 
552 #define LO(x) ((x) & 0x0000ffff)
553 #define HI(x) ((x) >> 16)
554 
555 	mtx_enter(&audio_lock);
556 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
557 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
558 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
559 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
560 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
561 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
562 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
563 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
564 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
565 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
566 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
567 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
568 
569 	/* DSP buffers */
570 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
571 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
572 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
573 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
574 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
575 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
576 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
577 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
578 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
579 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
580 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
581 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
582 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
583 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
584 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
585 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
586 
587 	/* Some per-client initializers */
588 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
589 	    ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8);
590 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
591 	    ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
592 	/* Enable or disable low-pass filter? (0xff if rate > 45000) */
593 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
594 	    ESA_SRC3_DIRECTION_OFFSET + 22,
595 	    vc->play.mode.sample_rate > 45000 ? 0xff : 0);
596 	/* Tell it which way DMA is going */
597 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
598 	    ESA_CDATA_DMA_CONTROL,
599 	    ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
600 	    ESA_DMAC_BLOCKF_SELECTOR);
601 
602 	/* Set an armload of static initializers */
603 	for (i = 0; i < nitems(esa_playvals); i++)
604 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
605 		    esa_playvals[i].addr, esa_playvals[i].val);
606 
607 	/* Put us in the packed task lists */
608 	esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT,
609 		     vc->index);
610 	esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT,
611 		     vc->index);
612 	esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT,
613 		     vc->index);
614 #undef LO
615 #undef HI
616 
617 	/* XXX */
618 	//esa_commit_settings(vc);
619 
620 	sc->sc_ntimers++;
621 
622 	if (sc->sc_ntimers == 1) {
623 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
624 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
625 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
626 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
627 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
628 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
629 		    data | ESA_CLKRUN_GEN_ENABLE);
630 	}
631 
632 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
633 	    ESA_CDATA_INSTANCE_READY, 1);
634 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
635 	    ESA_KDATA_MIXER_TASK_NUMBER,
636 	    sc->mixer_list.indexmap[vc->index]);
637 	mtx_leave(&audio_lock);
638 	return (0);
639 }
640 
641 int
642 esa_trigger_input(void *hdl, void *start, void *end, int blksize,
643 			void (*intr)(void *), void *intrarg,
644 			struct audio_params *param)
645 {
646 	struct esa_voice *vc = hdl;
647 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
648 	struct esa_dma *p;
649 	bus_space_tag_t iot = sc->sc_iot;
650 	bus_space_handle_t ioh = sc->sc_ioh;
651 	u_int32_t data;
652 	u_int32_t bufaddr;
653 	u_int32_t i;
654 	size_t size;
655 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
656 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
657 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
658 			   &~ 255;
659 	int adc_data = ESA_DAC_DATA + (data_bytes * vc->index) +
660 		       (data_bytes / 2);
661 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
662 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
663 	int dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
664 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
665 	vc->rec.data_offset = adc_data;
666 
667 	/* We only support 1 recording channel */
668 	if (vc->index > 0)
669 		return (ENODEV);
670 
671 	if (vc->rec.active)
672 		return (EINVAL);
673 
674 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
675 		;
676 	if (!p) {
677 		printf("%s: esa_trigger_input: bad addr %p\n",
678 		    sc->sc_dev.dv_xname, start);
679 		return (EINVAL);
680 	}
681 
682 	vc->rec.active = 1;
683 	vc->rec.intr = intr;
684 	vc->rec.arg = intrarg;
685 	vc->rec.pos = 0;
686 	vc->rec.count = 0;
687 	vc->rec.buf = start;
688 	size = (size_t)(((caddr_t)end - (caddr_t)start));
689 	bufaddr = DMAADDR(p);
690 	vc->rec.start = bufaddr;
691 
692 #define LO(x) ((x) & 0x0000ffff)
693 #define HI(x) ((x) >> 16)
694 	mtx_enter(&audio_lock);
695 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
696 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
697 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
698 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
699 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
700 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
701 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
702 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
703 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
704 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
705 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
706 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
707 
708 	/* DSP buffers */
709 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
710 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
711 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
712 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
713 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
714 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
715 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
716 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
717 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
718 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
719 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
720 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
721 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
722 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
723 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
724 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
725 
726 	/* Some per-client initializers */
727 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
728 	    ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8);
729 	/* Tell it which way DMA is going */
730 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
731 	    ESA_CDATA_DMA_CONTROL,
732 	    ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT +
733 	    ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR);
734 
735 	/* Set an armload of static initializers */
736 	for (i = 0; i < nitems(esa_recvals); i++)
737 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
738 		    esa_recvals[i].addr, esa_recvals[i].val);
739 
740 	/* Put us in the packed task lists */
741 	esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT,
742 		     vc->index + ESA_NUM_VOICES);
743 	esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT,
744 		     vc->index + ESA_NUM_VOICES);
745 	esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT,
746 		     vc->index + ESA_NUM_VOICES);
747 #undef LO
748 #undef HI
749 
750 	/* XXX */
751 	//esa_commit_settings(vc);
752 
753 	sc->sc_ntimers++;
754 	if (sc->sc_ntimers == 1) {
755 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
756 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
757 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
758 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
759 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
760 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
761 		    data | ESA_CLKRUN_GEN_ENABLE);
762 	}
763 
764 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
765 	    ESA_CDATA_INSTANCE_READY, 1);
766 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
767 	    1);
768 	mtx_leave(&audio_lock);
769 	return (0);
770 }
771 
772 /* Interrupt handler */
773 
774 int
775 esa_intr(void *hdl)
776 {
777 	struct esa_softc *sc = hdl;
778 	struct esa_voice *vc;
779 	bus_space_tag_t iot = sc->sc_iot;
780 	bus_space_handle_t ioh = sc->sc_ioh;
781 	u_int8_t status, ctl;
782 	u_int32_t pos;
783 	u_int32_t diff;
784 	u_int32_t play_blksize, play_bufsize;
785 	u_int32_t rec_blksize, rec_bufsize;
786 	int i, claimed = 0;
787 
788 	mtx_enter(&audio_lock);
789 	status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
790 	if (status == 0xff) {
791 		mtx_leave(&audio_lock);
792 		return (0);
793 	}
794 
795 	/* ack the interrupt */
796 	bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status);
797 
798 	if (status & ESA_HV_INT_PENDING) {
799 		u_int8_t event;
800 
801 		printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname);
802 		event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
803 		switch(event) {
804 		case 0x99:
805 		case 0xaa:
806 		case 0x66:
807 		case 0x88:
808 			printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname);
809 			break;
810 		default:
811 			printf("%s: unknown hwvol event 0x%02x\n",
812 			    sc->sc_dev.dv_xname, event);
813 			break;
814 		}
815 		bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
816 		claimed = 1;
817 	}
818 
819 	if (status & ESA_ASSP_INT_PENDING) {
820 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
821 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
822 			ctl = bus_space_read_1(iot, ioh,
823 					       ESA_ASSP_HOST_INT_STATUS);
824 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
825 				bus_space_write_1(iot, ioh,
826 				    ESA_ASSP_HOST_INT_STATUS,
827 				    ESA_DSP2HOST_REQ_TIMER);
828 				for (i = 0; i < ESA_NUM_VOICES; i++) {
829 					vc = &sc->voice[i];
830 					if (vc->play.active) {
831 						play_blksize = vc->play.blksize;
832 						play_bufsize = vc->play.bufsize;
833 						pos = esa_get_pointer(sc, &vc->play)
834 						    % play_bufsize;
835 						diff = (play_bufsize + pos - vc->play.pos)
836 						    % play_bufsize;
837 						vc->play.pos = pos;
838 						vc->play.count += diff;
839 						while(vc->play.count >= play_blksize) {
840 							vc->play.count -= play_blksize;
841 							(*vc->play.intr)(vc->play.arg);
842 						}
843 					}
844 					if (vc->rec.active) {
845 						rec_blksize = vc->rec.blksize;
846 						rec_bufsize = vc->rec.bufsize;
847 						pos = esa_get_pointer(sc, &vc->rec)
848 						    % rec_bufsize;
849 						diff = (rec_bufsize + pos - vc->rec.pos)
850 						    % rec_bufsize;
851 						vc->rec.pos = pos;
852 						vc->rec.count += diff;
853 						while(vc->rec.count >= rec_blksize) {
854 							vc->rec.count -= rec_blksize;
855 							(*vc->rec.intr)(vc->rec.arg);
856 						}
857 					}
858 				}
859 			}
860 		}
861 		claimed = 1;
862 	}
863 	mtx_leave(&audio_lock);
864 	return (claimed);
865 }
866 
867 int
868 esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
869 		struct esa_dma *p)
870 {
871 	int error;
872 
873 	p->size = size;
874 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
875 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
876 				 &p->nsegs, BUS_DMA_NOWAIT);
877 	if (error)
878 		return (error);
879 
880 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
881 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
882 	if (error)
883 		goto free;
884 
885 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
886 				  BUS_DMA_NOWAIT, &p->map);
887 	if (error)
888 		goto unmap;
889 
890 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
891 				BUS_DMA_NOWAIT);
892 	if (error)
893 		goto destroy;
894 
895 	return (0);
896 
897 destroy:
898 	bus_dmamap_destroy(sc->sc_dmat, p->map);
899 unmap:
900 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
901 free:
902 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
903 
904 	return (error);
905 }
906 
907 int
908 esa_freemem(struct esa_softc *sc, struct esa_dma *p)
909 {
910 
911 	bus_dmamap_unload(sc->sc_dmat, p->map);
912 	bus_dmamap_destroy(sc->sc_dmat, p->map);
913 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
914 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
915 
916 	return (0);
917 }
918 
919 /*
920  * Supporting Subroutines
921  */
922 const struct pci_matchid esa_devices[] = {
923 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989 },
924 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3 },
925 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2 },
926 };
927 
928 int
929 esa_match(struct device *dev, void *match, void *aux)
930 {
931 	return (pci_matchbyid((struct pci_attach_args *)aux, esa_devices,
932 	    nitems(esa_devices)));
933 }
934 
935 void
936 esa_attach(struct device *parent, struct device *self, void *aux)
937 {
938 	struct esa_softc *sc = (struct esa_softc *)self;
939 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
940 	pcitag_t tag = pa->pa_tag;
941 	pci_chipset_tag_t pc = pa->pa_pc;
942 	pci_intr_handle_t ih;
943 	struct esa_card_type *card;
944 	const char *intrstr;
945 	int i, len;
946 
947 	for (card = esa_card_types; card->pci_vendor_id; card++)
948 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
949 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
950 			sc->type = card->type;
951 			sc->delay1 = card->delay1;
952 			sc->delay2 = card->delay2;
953 			break;
954 		}
955 
956 	/* Map I/O register */
957 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
958 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) {
959 		printf(": can't map i/o space\n");
960 		return;
961 	}
962 
963 	/* Initialize softc */
964 	sc->sc_tag = tag;
965 	sc->sc_pct = pc;
966 	sc->sc_dmat = pa->pa_dmat;
967 
968 	/* Map and establish an interrupt */
969 	if (pci_intr_map(pa, &ih)) {
970 		printf(": can't map interrupt\n");
971 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
972 		return;
973 	}
974 	intrstr = pci_intr_string(pc, ih);
975 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE,
976 	    esa_intr, self, sc->sc_dev.dv_xname);
977 	if (sc->sc_ih == NULL) {
978 		printf(": can't establish interrupt");
979 		if (intrstr != NULL)
980 			printf(" at %s", intrstr);
981 		printf("\n");
982 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
983 		return;
984 	}
985 	printf(": %s\n", intrstr);
986 
987 	/* Power up chip */
988 	pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0);
989 
990 	/* Init chip */
991 	if (esa_init(sc) == -1) {
992 		printf("%s: esa_attach: unable to initialize the card\n",
993 		    sc->sc_dev.dv_xname);
994 		pci_intr_disestablish(pc, sc->sc_ih);
995 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
996 		return;
997 	}
998 
999 	/* create suspend save area */
1000 	len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1001 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1002 	sc->savemem = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1003 	if (sc->savemem == NULL) {
1004 		printf("%s: unable to allocate suspend buffer\n",
1005 		    sc->sc_dev.dv_xname);
1006 		pci_intr_disestablish(pc, sc->sc_ih);
1007 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1008 		return;
1009 	}
1010 
1011 	/*
1012 	 * Every card I've seen has had their channels swapped with respect
1013 	 * to the mixer. Ie:
1014 	 *  $ mixerctl -w outputs.master=0,191
1015 	 * Would result in the _right_ speaker being turned off.
1016 	 *
1017 	 * So, we will swap the left and right mixer channels to compensate
1018 	 * for this.
1019 	 */
1020 	sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS;
1021 	sc->codec_flags |= AC97_HOST_DONT_READ;
1022 
1023 	/* Attach AC97 host interface */
1024 	sc->host_if.arg = self;
1025 	sc->host_if.attach = esa_attach_codec;
1026 	sc->host_if.read = esa_read_codec;
1027 	sc->host_if.write = esa_write_codec;
1028 	sc->host_if.reset = esa_reset_codec;
1029 	sc->host_if.flags = esa_flags_codec;
1030 
1031 	if (ac97_attach(&sc->host_if) != 0) {
1032 		pci_intr_disestablish(pc, sc->sc_ih);
1033 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1034 		free(sc->savemem, M_DEVBUF, 0);
1035 		return;
1036 	}
1037 
1038 	/* initialize list management structures */
1039 	sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0;
1040 	sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS;
1041 	sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0;
1042 	sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS;
1043 	sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0;
1044 	sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS;
1045 	sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC;
1046 	sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC;
1047 
1048 	/* initialize index maps */
1049 	for (i = 0; i < ESA_NUM_VOICES * 2; i++) {
1050 		sc->mixer_list.indexmap[i] = -1;
1051 		sc->msrc_list.indexmap[i] = -1;
1052 		sc->dma_list.indexmap[i] = -1;
1053 		sc->adc1_list.indexmap[i] = -1;
1054 	}
1055 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1056 		sc->voice[i].parent = (struct device *)sc;
1057 		sc->voice[i].index = i;
1058 		sc->sc_audiodev[i] =
1059 		    audio_attach_mi(&esa_hw_if, &sc->voice[i], &sc->sc_dev);
1060 	}
1061 }
1062 
1063 int
1064 esa_detach(struct device *self, int flags)
1065 {
1066 	struct esa_softc *sc = (struct esa_softc *)self;
1067 	int i;
1068 
1069 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1070 		if (sc->sc_audiodev[i] != NULL)
1071 			config_detach(sc->sc_audiodev[i], flags);
1072 	}
1073 
1074 	if (sc->sc_ih != NULL)
1075 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1076 	if (sc->sc_ios)
1077 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1078 
1079 	free(sc->savemem, M_DEVBUF, 0);
1080 
1081 	return (0);
1082 }
1083 
1084 u_int16_t
1085 esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
1086 {
1087 	u_int16_t data;
1088 	bus_space_tag_t iot = sc->sc_iot;
1089 	bus_space_handle_t ioh = sc->sc_ioh;
1090 
1091 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1092 	    region & ESA_MEMTYPE_MASK);
1093 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1094 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
1095 
1096 	return (data);
1097 }
1098 
1099 void
1100 esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
1101 		u_int16_t data)
1102 {
1103 	bus_space_tag_t iot = sc->sc_iot;
1104 	bus_space_handle_t ioh = sc->sc_ioh;
1105 
1106 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1107 	    region & ESA_MEMTYPE_MASK);
1108 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1109 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
1110 
1111 	return;
1112 }
1113 
1114 int
1115 esa_init_codec(struct esa_softc *sc)
1116 {
1117 	bus_space_tag_t iot = sc->sc_iot;
1118 	bus_space_handle_t ioh = sc->sc_ioh;
1119 	u_int32_t data;
1120 
1121 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
1122 
1123 	return ((data & 0x1) ? 0 : 1);
1124 }
1125 
1126 int
1127 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1128 {
1129 	struct esa_softc *sc = aux;
1130 
1131 	sc->codec_if = codec_if;
1132 
1133 	return (0);
1134 }
1135 
1136 int
1137 esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
1138 {
1139 	struct esa_softc *sc = aux;
1140 	bus_space_tag_t iot = sc->sc_iot;
1141 	bus_space_handle_t ioh = sc->sc_ioh;
1142 
1143 	if (esa_wait(sc))
1144 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1145 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
1146 	delay(50);
1147 	if (esa_wait(sc))
1148 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1149 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
1150 
1151 	return (0);
1152 }
1153 
1154 int
1155 esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
1156 {
1157 	struct esa_softc *sc = aux;
1158 	bus_space_tag_t iot = sc->sc_iot;
1159 	bus_space_handle_t ioh = sc->sc_ioh;
1160 
1161 	if (esa_wait(sc)) {
1162 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
1163 		return (-1);
1164 	}
1165 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
1166 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
1167 	delay(50);
1168 
1169 	return (0);
1170 }
1171 
1172 void
1173 esa_reset_codec(void *aux)
1174 {
1175 
1176 	return;
1177 }
1178 
1179 enum ac97_host_flags
1180 esa_flags_codec(void *aux)
1181 {
1182 	struct esa_softc *sc = aux;
1183 
1184 	return (sc->codec_flags);
1185 }
1186 
1187 int
1188 esa_wait(struct esa_softc *sc)
1189 {
1190 	int i, val;
1191 	bus_space_tag_t iot = sc->sc_iot;
1192 	bus_space_handle_t ioh = sc->sc_ioh;
1193 
1194 	for (i = 0; i < 20; i++) {
1195 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
1196 		if ((val & 1) == 0)
1197 			return (0);
1198 		delay(2);
1199 	}
1200 
1201 	return (-1);
1202 }
1203 
1204 int
1205 esa_init(struct esa_softc *sc)
1206 {
1207 	struct esa_voice *vc;
1208 	bus_space_tag_t iot = sc->sc_iot;
1209 	bus_space_handle_t ioh = sc->sc_ioh;
1210 	pcitag_t tag = sc->sc_tag;
1211 	pci_chipset_tag_t pc = sc->sc_pct;
1212 	u_int32_t data, i, size;
1213 	u_int8_t reset_state;
1214 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
1215 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
1216 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
1217 			   &~ 255;
1218 
1219 	/* Disable legacy emulation */
1220 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
1221 	data |= DISABLE_LEGACY;
1222 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
1223 
1224 	esa_config(sc);
1225 
1226 	reset_state = esa_assp_halt(sc);
1227 
1228 	esa_init_codec(sc);
1229 	esa_codec_reset(sc);
1230 
1231 	/* Zero kernel and mixer data */
1232 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
1233 	for (i = 0; i < size / 2; i++) {
1234 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1235 		    ESA_KDATA_BASE_ADDR + i, 0);
1236 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1237 		    ESA_KDATA_BASE_ADDR2 + i, 0);
1238 	}
1239 
1240 	/* Init DMA pointer */
1241 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
1242 	    ESA_KDATA_DMA_XFER0);
1243 
1244 	/* Write kernel code into memory */
1245 	size = nitems(esa_assp_kernel_image);
1246 	for (i = 0; i < size; i++)
1247 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1248 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
1249 
1250 	size = nitems(esa_assp_minisrc_image);
1251 	for (i = 0; i < size; i++)
1252 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
1253 		    esa_assp_minisrc_image[i]);
1254 
1255 	/* Write the coefficients for the low pass filter */
1256 	size = nitems(esa_minisrc_lpf_image);
1257 	for (i = 0; i < size; i++)
1258 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1259 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
1260 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1261 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
1262 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
1263 	/* Init the mixer number */
1264 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1265              ESA_KDATA_MIXER_TASK_NUMBER, 0);
1266 	/* Extreme kernel master volume */
1267 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1268 	    ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME);
1269 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1270             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
1271 
1272 	if (esa_amp_enable(sc))
1273 		return (-1);
1274 
1275 	/* Zero entire DAC/ADC area */
1276 	for (i = 0x1100; i < 0x1c00; i++)
1277 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
1278 
1279 	/* set some sane defaults */
1280 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1281 		vc = &sc->voice[i];
1282 		vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i);
1283 		vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2);
1284 	}
1285 
1286 	esa_enable_interrupts(sc);
1287 
1288 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1289 	    reset_state | ESA_REGB_ENABLE_RESET);
1290 
1291 	return (0);
1292 }
1293 
1294 void
1295 esa_config(struct esa_softc *sc)
1296 {
1297 	bus_space_tag_t iot = sc->sc_iot;
1298 	bus_space_handle_t ioh = sc->sc_ioh;
1299 	pcitag_t tag = sc->sc_tag;
1300 	pci_chipset_tag_t pc = sc->sc_pct;
1301 	u_int32_t data;
1302 
1303 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1304 	data &= ESA_REDUCED_DEBOUNCE;
1305 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
1306 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1307 
1308 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
1309 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1310 	data &= ~ESA_INT_CLK_SELECT;
1311 	if (sc->type == ESS_MAESTRO3) {
1312 		data &= ~ESA_INT_CLK_MULT_ENABLE;
1313 		data |= ESA_INT_CLK_SRC_NOT_PCI;
1314 	}
1315 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
1316 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1317 
1318 	if (sc->type == ESS_ALLEGRO1) {
1319 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
1320 		data |= ESA_IN_CLK_12MHZ_SELECT;
1321 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
1322 	}
1323 
1324 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
1325 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
1326 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
1327 	data |= ESA_ASSP_0_WS_ENABLE;
1328 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
1329 
1330 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
1331 
1332 	return;
1333 }
1334 
1335 u_int8_t
1336 esa_assp_halt(struct esa_softc *sc)
1337 {
1338 	bus_space_tag_t iot = sc->sc_iot;
1339 	bus_space_handle_t ioh = sc->sc_ioh;
1340 	u_int8_t data, reset_state;
1341 
1342 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
1343 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
1344 	delay(10000);		/* XXX use tsleep */
1345 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1346 			reset_state & ~ESA_REGB_ENABLE_RESET);
1347 	delay(10000);		/* XXX use tsleep */
1348 
1349 	return (reset_state);
1350 }
1351 
1352 void
1353 esa_codec_reset(struct esa_softc *sc)
1354 {
1355 	bus_space_tag_t iot = sc->sc_iot;
1356 	bus_space_handle_t ioh = sc->sc_ioh;
1357 	u_int16_t data, dir;
1358 	int retry = 0;
1359 
1360 	do {
1361 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1362 		dir = data | 0x10; /* assuming pci bus master? */
1363 
1364 		/* remote codec config */
1365 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
1366 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
1367 		    data & ~ESA_SECOND_CODEC_ID_MASK);
1368 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
1369 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
1370 		    data & ~ESA_COMMAND_ADDR_OUT);
1371 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
1372 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
1373 		    data & ~ESA_STATUS_ADDR_IN);
1374 
1375 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1376 				  ESA_IO_SRAM_ENABLE);
1377 		delay(20);
1378 
1379 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1380 		    dir & ~ESA_GPO_PRIMARY_AC97);
1381 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
1382 				  ~ESA_GPO_PRIMARY_AC97);
1383 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
1384 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1385 		    dir | ESA_GPO_PRIMARY_AC97);
1386 		delay(sc->delay1 * 1000);
1387 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
1388 				  ESA_GPO_PRIMARY_AC97);
1389 		delay(5);
1390 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1391 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
1392 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1393 		delay(sc->delay2 * 1000);
1394 
1395 		esa_read_codec(sc, 0x7c, &data);
1396 		if ((data == 0) || (data == 0xffff)) {
1397 			retry++;
1398 			if (retry > 3) {
1399 				printf("%s: esa_codec_reset: failed\n",
1400 				    sc->sc_dev.dv_xname);
1401 				break;
1402 			}
1403 			printf("%s: esa_codec_reset: retrying\n",
1404 			    sc->sc_dev.dv_xname);
1405 		} else
1406 			retry = 0;
1407 	} while (retry);
1408 
1409 	return;
1410 }
1411 
1412 int
1413 esa_amp_enable(struct esa_softc *sc)
1414 {
1415 	bus_space_tag_t iot = sc->sc_iot;
1416 	bus_space_handle_t ioh = sc->sc_ioh;
1417 	u_int32_t gpo, polarity_port, polarity;
1418 	u_int16_t data;
1419 
1420 	switch (sc->type) {
1421 	case ESS_ALLEGRO1:
1422 		polarity_port = 0x1800;
1423 		break;
1424 	case ESS_MAESTRO3:
1425 		polarity_port = 0x1100;
1426 		break;
1427 	default:
1428 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
1429 		    sc->sc_dev.dv_xname);
1430 		return (1);
1431 	}
1432 
1433 	gpo = (polarity_port >> 8) & 0x0f;
1434 	polarity = polarity_port >> 12;
1435 	polarity = !polarity;	/* Enable */
1436 	polarity = polarity << gpo;
1437 	gpo = 1 << gpo;
1438 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
1439 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1440 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
1441 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
1442 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
1443 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1444 
1445 	return (0);
1446 }
1447 
1448 void
1449 esa_enable_interrupts(struct esa_softc *sc)
1450 {
1451 	bus_space_tag_t iot = sc->sc_iot;
1452 	bus_space_handle_t ioh = sc->sc_ioh;
1453 	u_int8_t data;
1454 
1455 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
1456 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
1457 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
1458 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
1459 	    data | ESA_ASSP_HOST_INT_ENABLE);
1460 }
1461 
1462 /*
1463  * List management
1464  */
1465 int
1466 esa_add_list(struct esa_voice *vc, struct esa_list *el,
1467 	     u_int16_t val, int index)
1468 {
1469 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1470 
1471 	el->indexmap[index] = el->currlen;
1472 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1473 		       el->mem_addr + el->currlen,
1474 		       val);
1475 
1476 	return (el->currlen++);
1477 }
1478 
1479 void
1480 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index)
1481 {
1482 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1483 	u_int16_t val;
1484 	int lastindex = el->currlen - 1;
1485 	int vindex = el->indexmap[index];
1486 	int i;
1487 
1488 	/* reset our virtual index */
1489 	el->indexmap[index] = -1;
1490 
1491 	if (vindex != lastindex) {
1492 		val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1493 				    el->mem_addr + lastindex);
1494 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1495 			       el->mem_addr + vindex,
1496 			       val);
1497 		for (i = 0; i < ESA_NUM_VOICES * 2; i++)
1498 			if (el->indexmap[i] == lastindex)
1499 				break;
1500 		if (i >= ESA_NUM_VOICES * 2)
1501 			printf("%s: esa_remove_list: invalid task index\n",
1502 			       sc->sc_dev.dv_xname);
1503 		else
1504 			el->indexmap[i] = vindex;
1505 	}
1506 
1507 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1508 		       el->mem_addr + lastindex, 0);
1509 	el->currlen--;
1510 
1511 	return;
1512 }
1513 
1514 int
1515 esa_activate(struct device *self, int act)
1516 {
1517 	struct esa_softc *sc = (struct esa_softc *)self;
1518 
1519 	switch (act) {
1520 	case DVACT_SUSPEND:
1521 		esa_suspend(sc);
1522 		break;
1523 	case DVACT_RESUME:
1524 		esa_resume(sc);
1525 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1526 		break;
1527 	}
1528 	return 0;
1529 }
1530 
1531 int
1532 esa_suspend(struct esa_softc *sc)
1533 {
1534 	bus_space_tag_t iot = sc->sc_iot;
1535 	bus_space_handle_t ioh = sc->sc_ioh;
1536 	int i, index;
1537 
1538 	index = 0;
1539 
1540 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
1541 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
1542 
1543 	esa_assp_halt(sc);
1544 
1545 	/* Save ASSP state */
1546 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1547 	    i++)
1548 		sc->savemem[index++] = esa_read_assp(sc,
1549 		    ESA_MEMTYPE_INTERNAL_CODE, i);
1550 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1551 	    i++)
1552 		sc->savemem[index++] = esa_read_assp(sc,
1553 		    ESA_MEMTYPE_INTERNAL_DATA, i);
1554 
1555 	return (0);
1556 }
1557 
1558 int
1559 esa_resume(struct esa_softc *sc) {
1560 	bus_space_tag_t iot = sc->sc_iot;
1561 	bus_space_handle_t ioh = sc->sc_ioh;
1562 	int i, index;
1563 	u_int8_t reset_state;
1564 
1565 	index = 0;
1566 
1567 	esa_config(sc);
1568 
1569 	reset_state = esa_assp_halt(sc);
1570 
1571 	esa_codec_reset(sc);
1572 
1573 	/* restore ASSP */
1574 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1575 	    i++)
1576 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
1577 		    sc->savemem[index++]);
1578 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1579 	    i++)
1580 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
1581 		    sc->savemem[index++]);
1582 
1583 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
1584 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1585 	    reset_state | ESA_REGB_ENABLE_RESET);
1586 
1587 	esa_enable_interrupts(sc);
1588 	esa_amp_enable(sc);
1589 
1590 	return (0);
1591 }
1592 
1593 u_int32_t
1594 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
1595 {
1596 	u_int16_t hi = 0, lo = 0;
1597 	u_int32_t addr;
1598 	int data_offset = ch->data_offset;
1599 
1600 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1601 	    ESA_CDATA_HOST_SRC_CURRENTH);
1602 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1603 	    ESA_CDATA_HOST_SRC_CURRENTL);
1604 
1605 	addr = lo | ((u_int32_t)hi << 16);
1606 	return (addr - ch->start);
1607 }
1608