xref: /netbsd-src/sys/dev/pci/auixp.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /* $NetBSD: auixp.c,v 1.14 2006/06/17 23:34:26 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005 Reinoud Zandijk <reinoud@netbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the NetBSD
17  *	Foundation, Inc. and its contributors.
18  * 4. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 
36 /*
37  * NetBSD audio driver for ATI IXP-{150,200,...} audio driver hardware.
38  *
39  * Recording and playback has been tested OK on various sample rates and
40  * encodings.
41  *
42  * Known problems and issues :
43  * - SPDIF is untested and needs some work still (LED stays off)
44  * - 32 bit audio playback failed last time i tried but that might an AC'97
45  *   codec support problem.
46  * - 32 bit recording works but can't try out playing: see above.
47  * - no suspend/resume support yet.
48  * - multiple codecs are `supported' but not tested; the implemetation needs
49  *   some cleaning up.
50  */
51 
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: auixp.c,v 1.14 2006/06/17 23:34:26 christos Exp $");
54 
55 #include <sys/types.h>
56 #include <sys/errno.h>
57 #include <sys/null.h>
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/device.h>
62 #include <sys/conf.h>
63 #include <sys/exec.h>
64 #include <sys/select.h>
65 #include <sys/audioio.h>
66 #include <sys/queue.h>
67 
68 #include <machine/bus.h>
69 #include <machine/intr.h>
70 
71 #include <dev/pci/pcidevs.h>
72 #include <dev/pci/pcivar.h>
73 
74 #include <dev/audio_if.h>
75 #include <dev/mulaw.h>
76 #include <dev/auconv.h>
77 #include <dev/ic/ac97var.h>
78 #include <dev/ic/ac97reg.h>
79 
80 #include <dev/pci/auixpreg.h>
81 #include <dev/pci/auixpvar.h>
82 
83 
84 //#define DEBUG_AUIXP
85 
86 
87 /* why isn't this base address register not in the headerfile? */
88 #define PCI_CBIO 0x10
89 
90 
91 /* macro's used */
92 #define KERNADDR(p)	((void *)((p)->addr))
93 #define	DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
94 
95 
96 /* the differences might be irrelevant */
97 enum {
98 	IXP_200,
99 	IXP_300,
100 	IXP_400
101 };
102 
103 
104 /* our `cards' */
105 static const struct auixp_card_type {
106 	uint16_t pci_vendor_id;
107 	uint16_t pci_product_id;
108 	int type;
109 } auixp_card_types[] = {
110 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_200, IXP_200 },
111 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_300, IXP_300 },
112 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_400, IXP_400 },
113 	{ 0, 0, 0 }
114 };
115 
116 
117 struct audio_device auixp_device = {
118 	"ATI IXP audio",
119 	"",
120 	"auixp"
121 };
122 
123 
124 /* codec detection constant indicating the interrupt flags */
125 #define ALL_CODECS_NOT_READY \
126 	    (ATI_REG_ISR_CODEC0_NOT_READY |\
127 	     ATI_REG_ISR_CODEC1_NOT_READY |\
128 	     ATI_REG_ISR_CODEC2_NOT_READY)
129 #define CODEC_CHECK_BITS (ALL_CODECS_NOT_READY|ATI_REG_ISR_NEW_FRAME)
130 
131 
132 /* autoconfig */
133 static int	auixp_match( struct device *, struct cfdata *, void *);
134 static void	auixp_attach(struct device *, struct device *, void *);
135 static int	auixp_detach(struct device *, int);
136 
137 
138 /* audio(9) function prototypes */
139 static int	auixp_query_encoding(void *, struct audio_encoding *);
140 static int	auixp_set_params(void *, int, int, audio_params_t *,
141 				 audio_params_t *,
142 		stream_filter_list_t *, stream_filter_list_t *);
143 static int	auixp_commit_settings(void *);
144 static int	auixp_round_blocksize(void *, int, int, const audio_params_t *);
145 static int	auixp_trigger_output(void *, void *, void *, int,
146 				     void (*)(void *),
147 		void *, const audio_params_t *);
148 static int	auixp_trigger_input(void *, void *, void *, int,
149 				    void (*)(void *),
150 		void *, const audio_params_t *);
151 static int	auixp_halt_output(void *);
152 static int	auixp_halt_input(void *);
153 static int	auixp_set_port(void *, mixer_ctrl_t *);
154 static int	auixp_get_port(void *, mixer_ctrl_t *);
155 static int	auixp_query_devinfo(void *, mixer_devinfo_t *);
156 static void *	auixp_malloc(void *, int, size_t, struct malloc_type *, int);
157 static void	auixp_free(void *, void *, struct malloc_type *);
158 static int	auixp_getdev(void *, struct audio_device *);
159 static size_t	auixp_round_buffersize(void *, int, size_t);
160 static int	auixp_get_props(void *);
161 static int	auixp_intr(void *);
162 static int	auixp_allocmem(struct auixp_softc *, size_t, size_t,
163 		struct auixp_dma *);
164 static int	auixp_freemem(struct auixp_softc *, struct auixp_dma *);
165 static paddr_t	auixp_mappage(void *, void *, off_t, int);
166 
167 
168 /* power management (do we support that already?) */
169 #if 0
170 static void	auixp_powerhook(int, void *);
171 static int	auixp_suspend(struct auixp_softc *);
172 static int	auixp_resume(struct auixp_softc *);
173 #endif
174 
175 
176 /* Supporting subroutines */
177 static int	auixp_init(struct auixp_softc *);
178 static void	auixp_autodetect_codecs(struct auixp_softc *);
179 static void	auixp_post_config(struct device *);
180 
181 static void	auixp_reset_aclink(struct auixp_softc *);
182 static int	auixp_attach_codec(void *, struct ac97_codec_if *);
183 static int	auixp_read_codec(void *, uint8_t, uint16_t *);
184 static int	auixp_write_codec(void *, uint8_t, uint16_t);
185 static int	auixp_wait_for_codecs(struct auixp_softc *, const char *);
186 static int	auixp_reset_codec(void *);
187 static enum ac97_host_flags	auixp_flags_codec(void *);
188 
189 static void	auixp_enable_dma(struct auixp_softc *, struct auixp_dma *);
190 static void	auixp_disable_dma(struct auixp_softc *, struct auixp_dma *);
191 static void	auixp_enable_interrupts(struct auixp_softc *);
192 static void	auixp_disable_interrupts(struct auixp_softc *);
193 
194 
195 /* statics */
196 static void	auixp_link_daisychain(struct auixp_softc *,
197 				      struct auixp_dma *, struct auixp_dma *,
198 				      int, int);
199 static int	auixp_allocate_dma_chain(struct auixp_softc *,
200 					 struct auixp_dma **);
201 static void	auixp_program_dma_chain(struct auixp_softc *,
202 					struct auixp_dma *);
203 static void	auixp_dma_update(struct auixp_softc *, struct auixp_dma *);
204 static void	auixp_update_busbusy(struct auixp_softc *);
205 
206 
207 #ifdef DEBUG_AUIXP
208 static struct auixp_softc *static_sc;
209 sdtatic void auixp_dumpreg(void);
210 #	define DPRINTF(x) printf x;
211 #else
212 #	define DPRINTF(x)
213 #endif
214 
215 
216 static const struct audio_hw_if auixp_hw_if = {
217 	NULL,			/* open */
218 	NULL,			/* close */
219 	NULL,			/* drain */
220 	auixp_query_encoding,
221 	auixp_set_params,
222 	auixp_round_blocksize,
223 	auixp_commit_settings,
224 	NULL,			/* init_output  */
225 	NULL,			/* init_input   */
226 	NULL,			/* start_output */
227 	NULL,			/* start_input  */
228 	auixp_halt_output,
229 	auixp_halt_input,
230 	NULL,			/* speaker_ctl */
231 	auixp_getdev,
232 	NULL,			/* getfd */
233 	auixp_set_port,
234 	auixp_get_port,
235 	auixp_query_devinfo,
236 	auixp_malloc,
237 	auixp_free,
238 	auixp_round_buffersize,
239 	auixp_mappage,
240 	auixp_get_props,
241 	auixp_trigger_output,
242 	auixp_trigger_input
243 };
244 
245 
246 CFATTACH_DECL(auixp, sizeof(struct auixp_softc), auixp_match, auixp_attach,
247     auixp_detach, NULL);
248 
249 
250 /*
251  * audio(9) functions
252  */
253 
254 static int
255 auixp_query_encoding(void *hdl, struct audio_encoding *ae)
256 {
257 	struct auixp_codec *co;
258 	struct auixp_softc *sc;
259 
260 	co = (struct auixp_codec *) hdl;
261 	sc = co->sc;
262 	return auconv_query_encoding(sc->sc_encodings, ae);
263 }
264 
265 
266 static int
267 auixp_set_rate(struct auixp_codec *co, int mode, u_int srate)
268 {
269 	int ret;
270 	u_int ratetmp;
271 
272 	ratetmp = srate;
273 	if (mode == AUMODE_RECORD) {
274 		ret = co->codec_if->vtbl->set_rate(co->codec_if,
275 			AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
276 		return ret;
277 	}
278 
279 	/* play mode */
280 	ret = co->codec_if->vtbl->set_rate(co->codec_if,
281 		AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
282 	if (ret)
283 		return ret;
284 
285 	ratetmp = srate;
286 	ret = co->codec_if->vtbl->set_rate(co->codec_if,
287 		AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
288 	if (ret)
289 		return ret;
290 
291 	ratetmp = srate;
292 	ret = co->codec_if->vtbl->set_rate(co->codec_if,
293 		AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
294 	return ret;
295 }
296 
297 
298 /* commit setting and program ATI IXP chip */
299 static int
300 auixp_commit_settings(void *hdl)
301 {
302 	struct auixp_codec *co;
303 	struct auixp_softc *sc;
304 	bus_space_tag_t    iot;
305 	bus_space_handle_t ioh;
306 	struct audio_params *params;
307 	uint32_t value;
308 
309 	/* XXX would it be better to stop interrupts first? XXX */
310 	co = (struct auixp_codec *) hdl;
311 	sc = co->sc;
312 	iot = sc->sc_iot;
313 	ioh = sc->sc_ioh;
314 
315 	/* process input settings */
316 	params = &sc->sc_play_params;
317 
318 	/* set input interleaving (precision) */
319 	value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
320 	value &= ~ATI_REG_CMD_INTERLEAVE_IN;
321 	if (params->precision <= 16)
322 		value |= ATI_REG_CMD_INTERLEAVE_IN;
323 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
324 
325 	/* process output settings */
326 	params = &sc->sc_play_params;
327 
328 	value  =  bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT);
329 	value &= ~ATI_REG_OUT_DMA_SLOT_MASK;
330 
331 	/* TODO SPDIF case for 8 channels */
332 	switch (params->channels) {
333 	case 6:
334 		value |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
335 			 ATI_REG_OUT_DMA_SLOT_BIT(8);
336 		/* fallthru */
337 	case 4:
338 		value |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
339 			 ATI_REG_OUT_DMA_SLOT_BIT(9);
340 		/* fallthru */
341 	default:
342 		value |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
343 			 ATI_REG_OUT_DMA_SLOT_BIT(4);
344 		break;
345 	}
346 	/* set output threshold */
347 	value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
348 	bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value);
349 
350 	/* set output interleaving (precision) */
351 	value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
352 	value &= ~ATI_REG_CMD_INTERLEAVE_OUT;
353 	if (params->precision <= 16)
354 		value |= ATI_REG_CMD_INTERLEAVE_OUT;
355 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
356 
357 	/* enable 6 channel reordering */
358 	value  =  bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER);
359 	value &= ~ATI_REG_6CH_REORDER_EN;
360 	if (params->channels == 6)
361 		value |= ATI_REG_6CH_REORDER_EN;
362 	bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value);
363 
364 	if (sc->has_spdif) {
365 		/* set SPDIF (if present) */
366 		value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
367 		value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK;
368 		value |=  ATI_REG_CMD_SPDF_CONFIG_34; /* NetBSD AC'97 default */
369 
370 		/* XXX this prolly is not nessisary unless splitted XXX */
371 		value &= ~ATI_REG_CMD_INTERLEAVE_SPDF;
372 		if (params->precision <= 16)
373 			value |= ATI_REG_CMD_INTERLEAVE_SPDF;
374 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
375 	}
376 
377 	return 0;
378 }
379 
380 
381 /* set audio properties in desired setting */
382 static int
383 auixp_set_params(void *hdl, int setmode, int usemode, audio_params_t *play,
384 		 audio_params_t *rec, stream_filter_list_t *pfil,
385 		 stream_filter_list_t *rfil)
386 {
387 	struct auixp_codec *co;
388 	struct auixp_softc *sc;
389 	audio_params_t *params;
390 	stream_filter_list_t *fil;
391 	int mode, index;
392 
393 	/*
394 	 * In current NetBSD AC'97 implementation, SPDF is linked to channel 3
395 	 * and 4 i.e. stereo output.
396 	 */
397 
398 	co = (struct auixp_codec *) hdl;
399 	sc = co->sc;
400 	for (mode = AUMODE_RECORD; mode != -1;
401 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
402 		if ((setmode & mode) == 0)
403 			continue;
404 
405 		params = (mode == AUMODE_PLAY) ? play :  rec;
406 		fil    = (mode == AUMODE_PLAY) ? pfil : rfil;
407 		if (params == NULL)
408 			continue;
409 
410 		/* AD1888 settings ... don't know the IXP limits */
411 		if (params->sample_rate < AUIXP_MINRATE)
412 			return EINVAL;
413 		if (params->sample_rate > AUIXP_MAXRATE)
414 			return EINVAL;
415 
416 		index = auconv_set_converter(sc->sc_formats, AUIXP_NFORMATS,
417 					     mode, params, TRUE, fil);
418 
419 		/* nothing found? */
420 		if (index < 0)
421 			return EINVAL;
422 
423 		/* not sure yet as to why i have to change params here */
424 		if (fil->req_size > 0)
425 			params = &fil->filters[0].param;
426 
427 		/* if variable speed and we can't set the desired rate, fail */
428 		if ((sc->sc_formats[index].frequency_type != 1) &&
429 		    auixp_set_rate(co, mode, params->sample_rate))
430 			return EINVAL;
431 
432 		/* preserve the settings */
433 		if (mode == AUMODE_PLAY)
434 			sc->sc_play_params = *params;
435 		if (mode == AUMODE_RECORD)
436 			sc->sc_rec_params  = *params;
437 	}
438 
439 	return 0;
440 }
441 
442 
443 /* called to translate a requested blocksize to a hw-possible one */
444 static int
445 auixp_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
446 {
447 	uint32_t new_bs;
448 
449 	new_bs = bs;
450 	/* Be conservative; align to 32 bytes and maximise it to 64 kb */
451 	/* 256 kb possible */
452 	if (new_bs > 0x10000)
453 		bs = 0x10000;			/* 64 kb max */
454 	new_bs = (bs & ~0x20);			/* 32 bytes align */
455 
456 	return new_bs;
457 }
458 
459 
460 /*
461  * allocate dma capable memory and record its information for later retrieval
462  * when we program the dma chain itself. The trigger routines passes on the
463  * kernel virtual address we return here as a reference to the mapping.
464  */
465 static void *
466 auixp_malloc(void *hdl, int direction, size_t size,
467 	     struct malloc_type *type, int flags)
468 {
469 	struct auixp_codec *co;
470 	struct auixp_softc *sc;
471 	struct auixp_dma *dma;
472 	int error;
473 
474 	co = (struct auixp_codec *) hdl;
475 	sc = co->sc;
476 	/* get us a auixp_dma structure */
477 	dma = malloc(sizeof(*dma), type, flags);
478 	if (!dma)
479 		return NULL;
480 
481 	/* get us a dma buffer itself */
482 	error = auixp_allocmem(sc, size, 16, dma);
483 	if (error) {
484 		free(dma, type);
485 		printf("%s: auixp_malloc: not enough memory\n",
486 		    sc->sc_dev.dv_xname);
487 
488 		return NULL;
489 	}
490 	SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain);
491 
492 	DPRINTF(("auixp_malloc: returning kern %p,   hw 0x%08x for %d bytes "
493 	    "in %d segs\n", KERNADDR(dma), (uint32_t) DMAADDR(dma), dma->size,
494 	    dma->nsegs)
495 	);
496 
497 	return KERNADDR(dma);
498 }
499 
500 
501 /*
502  * free and release dma capable memory we allocated before and remove its
503  * recording
504  */
505 static void
506 auixp_free(void *hdl, void *addr, struct malloc_type *type)
507 {
508 	struct auixp_codec *co;
509 	struct auixp_softc *sc;
510 	struct auixp_dma *dma;
511 
512 	co = (struct auixp_codec *) hdl;
513 	sc = co->sc;
514 	SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) {
515 		if (KERNADDR(dma) == addr) {
516 			SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma,
517 			    dma_chain);
518 			auixp_freemem(sc, dma);
519 			free(dma, type);
520 			return;
521 		}
522 	}
523 }
524 
525 
526 static int
527 auixp_getdev(void *hdl, struct audio_device *ret)
528 {
529 
530 	*ret = auixp_device;
531 	return 0;
532 }
533 
534 
535 /* pass request to AC'97 codec code */
536 static int
537 auixp_set_port(void *hdl, mixer_ctrl_t *mc)
538 {
539 	struct auixp_codec *co;
540 
541 	co = (struct auixp_codec *) hdl;
542 	return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc);
543 }
544 
545 
546 /* pass request to AC'97 codec code */
547 static int
548 auixp_get_port(void *hdl, mixer_ctrl_t *mc)
549 {
550 	struct auixp_codec *co;
551 
552 	co = (struct auixp_codec *) hdl;
553 	return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc);
554 }
555 
556 /* pass request to AC'97 codec code */
557 static int
558 auixp_query_devinfo(void *hdl, mixer_devinfo_t *di)
559 {
560 	struct auixp_codec *co;
561 
562 	co = (struct auixp_codec *) hdl;
563 	return co->codec_if->vtbl->query_devinfo(co->codec_if, di);
564 }
565 
566 
567 static size_t
568 auixp_round_buffersize(void *hdl, int direction, size_t bufsize)
569 {
570 
571 	/* XXX force maximum? i.e. 256 kb? */
572 	return bufsize;
573 }
574 
575 
576 static int
577 auixp_get_props(void *hdl)
578 {
579 
580 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
581 }
582 
583 
584 /*
585  * A dma descriptor has dma->nsegs segments defined in dma->segs set up when
586  * we claimed the memory.
587  *
588  * Due to our demand for one contiguous DMA area, we only have one segment. A
589  * c_dma structure is about 3 kb for the 256 entries we maximally program
590  * -arbitrary limit AFAIK- so all is most likely to be in one segment/page
591  * anyway.
592  *
593  * XXX ought to implement fragmented dma area XXX
594  *
595  * Note that _v variables depict kernel virtual addresses, _p variables depict
596  * physical addresses.
597  */
598 static void
599 auixp_link_daisychain(struct auixp_softc *sc,
600 		struct auixp_dma *c_dma, struct auixp_dma *s_dma,
601 		int blksize, int blocks)
602 {
603 	atiixp_dma_desc_t *caddr_v, *next_caddr_v;
604 	uint32_t caddr_p, next_caddr_p, saddr_p;
605 	int i;
606 
607 	/* just make sure we are not changing when its running */
608 	auixp_disable_dma(sc, c_dma);
609 
610 	/* setup dma chain start addresses */
611 	caddr_v = KERNADDR(c_dma);
612 	caddr_p = DMAADDR(c_dma);
613 	saddr_p = DMAADDR(s_dma);
614 
615 	/* program the requested number of blocks */
616 	for (i = 0; i < blocks; i++) {
617 		/* clear the block just in case */
618 		bzero(caddr_v, sizeof(atiixp_dma_desc_t));
619 
620 		/* round robin the chain dma addresses for its successor */
621 		next_caddr_v = caddr_v + 1;
622 		next_caddr_p = caddr_p + sizeof(atiixp_dma_desc_t);
623 
624 		if (i == blocks-1) {
625 			next_caddr_v = KERNADDR(c_dma);
626 			next_caddr_p = DMAADDR(c_dma);
627 		}
628 
629 		/* fill in the hardware dma chain descriptor in little-endian */
630 		caddr_v->addr   = htole32(saddr_p);
631 		caddr_v->status = htole16(0);
632 		caddr_v->size   = htole16((blksize >> 2)); /* in dwords (!!!) */
633 		caddr_v->next   = htole32(next_caddr_p);
634 
635 		/* advance slot */
636 		saddr_p += blksize;	/* XXX assuming contiguous XXX */
637 		caddr_v  = next_caddr_v;
638 		caddr_p  = next_caddr_p;
639 	}
640 }
641 
642 
643 static int
644 auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap)
645 {
646 	struct auixp_dma *dma;
647 	int error;
648 
649 	/* allocate keeper of dma area */
650 	*dmap = NULL;
651 	dma = malloc(sizeof(struct auixp_dma), M_DEVBUF, M_NOWAIT | M_ZERO);
652 	if (!dma)
653 		return ENOMEM;
654 
655 	/* allocate for daisychain of IXP hardware-dma descriptors */
656 	error = auixp_allocmem(sc, DMA_DESC_CHAIN * sizeof(atiixp_dma_desc_t),
657 	    16, dma);
658 	if (error) {
659 		printf("%s: can't malloc dma descriptor chain\n",
660 		    sc->sc_dev.dv_xname);
661 		free(dma, M_DEVBUF);
662 		return ENOMEM;
663 	}
664 
665 	/* return info and initialise structure */
666 	dma->intr    = NULL;
667 	dma->intrarg = NULL;
668 
669 	*dmap = dma;
670 	return 0;
671 }
672 
673 
674 /* program dma chain in it's link address descriptor */
675 static void
676 auixp_program_dma_chain(struct auixp_softc *sc, struct auixp_dma *dma)
677 {
678 	bus_space_tag_t    iot;
679 	bus_space_handle_t ioh;
680 	uint32_t value;
681 
682 	iot = sc->sc_iot;
683 	ioh = sc->sc_ioh;
684 	/* get hardware start address of DMA chain and set valid-flag in it */
685 	/* XXX allways at start? XXX */
686 	value = DMAADDR(dma);
687 	value = value | ATI_REG_LINKPTR_EN;
688 
689 	/* reset linkpointer */
690 	bus_space_write_4(iot, ioh, dma->linkptr, 0);
691 
692 	/* reset this DMA engine */
693 	auixp_disable_dma(sc, dma);
694 	auixp_enable_dma(sc, dma);
695 
696 	/* program new DMA linkpointer */
697 	bus_space_write_4(iot, ioh, dma->linkptr, value);
698 }
699 
700 
701 /* called from interrupt code to signal end of one dma-slot */
702 static void
703 auixp_dma_update(struct auixp_softc *sc, struct auixp_dma *dma)
704 {
705 
706 	/* be very paranoid */
707 	if (!dma)
708 		panic("auixp: update: dma = NULL");
709 	if (!dma->intr)
710 		panic("auixp: update: dma->intr = NULL");
711 
712 	/* request more input from upper layer */
713 	(*dma->intr)(dma->intrarg);
714 }
715 
716 
717 /*
718  * The magic `busbusy' bit that needs to be set when dma is active; allowing
719  * busmastering?
720  */
721 static void
722 auixp_update_busbusy(struct auixp_softc *sc)
723 {
724 	bus_space_tag_t    iot;
725 	bus_space_handle_t ioh;
726 	uint32_t value;
727 	int running;
728 
729 	iot = sc->sc_iot;
730 	ioh = sc->sc_ioh;
731 	/* set bus-busy flag when either recording or playing is performed */
732 	value  = bus_space_read_4(iot, ioh, ATI_REG_IER);
733 	value &= ~ATI_REG_IER_SET_BUS_BUSY;
734 
735 	running = ((sc->sc_output_dma->running) || (sc->sc_input_dma->running));
736 	if (running)
737 		value |= ATI_REG_IER_SET_BUS_BUSY;
738 
739 	bus_space_write_4(iot, ioh, ATI_REG_IER, value);
740 
741 }
742 
743 
744 /*
745  * Called from upper audio layer to request playing audio, only called once;
746  * audio is refilled by calling the intr() function when space is available
747  * again.
748  */
749 /* XXX allmost literaly a copy of trigger-input; could be factorised XXX */
750 static int
751 auixp_trigger_output(void *hdl, void *start, void *end, int blksize,
752 		void (*intr)(void *), void *intrarg, const audio_params_t *param)
753 {
754 	struct auixp_codec *co;
755 	struct auixp_softc *sc;
756 	struct auixp_dma   *chain_dma;
757 	struct auixp_dma   *sound_dma;
758 	uint32_t blocks;
759 
760 	co = (struct auixp_codec *) hdl;
761 	sc = co->sc;
762 	chain_dma = sc->sc_output_dma;
763 	/* add functions to call back */
764 	chain_dma->intr    = intr;
765 	chain_dma->intrarg = intrarg;
766 
767 	/*
768 	 * Program output DMA chain with blocks from [start...end] with
769 	 * blksize fragments.
770 	 *
771 	 * NOTE, we can assume its in one block since we asked for it to be in
772 	 * one contiguous blob; XXX change this? XXX
773 	 */
774 	blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
775 
776 	/* lookup `start' address in our list of DMA area's */
777 	SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
778 		if (KERNADDR(sound_dma) == start)
779 			break;
780 	}
781 
782 	/* not ours ? then bail out */
783 	if (!sound_dma) {
784 		printf("%s: auixp_trigger_output: bad sound addr %p\n",
785 		    sc->sc_dev.dv_xname, start);
786 		return EINVAL;
787 	}
788 
789 	/* link round-robin daisychain and program hardware */
790 	auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
791 	auixp_program_dma_chain(sc, chain_dma);
792 
793 	/* mark we are now able to run now */
794 	chain_dma->running = 1;
795 
796 	/* update bus-flags; XXX programs more flags XXX */
797 	auixp_update_busbusy(sc);
798 
799 	/* callbacks happen in interrupt routine */
800 	return 0;
801 }
802 
803 
804 /* halt output of audio, just disable it's dma and update bus state */
805 static int
806 auixp_halt_output(void *hdl)
807 {
808 	struct auixp_codec *co;
809 	struct auixp_softc *sc;
810 	struct auixp_dma   *dma;
811 
812 	co  = (struct auixp_codec *) hdl;
813 	sc  = co->sc;
814 	dma = sc->sc_output_dma;
815 	auixp_disable_dma(sc, dma);
816 
817 	dma->running = 0;
818 	auixp_update_busbusy(sc);
819 
820 	return 0;
821 }
822 
823 
824 /* XXX allmost literaly a copy of trigger-output; could be factorised XXX */
825 static int
826 auixp_trigger_input(void *hdl, void *start, void *end, int blksize,
827 		void (*intr)(void *), void *intrarg, const audio_params_t *param)
828 {
829 	struct auixp_codec *co;
830 	struct auixp_softc *sc;
831 	struct auixp_dma   *chain_dma;
832 	struct auixp_dma   *sound_dma;
833 	uint32_t blocks;
834 
835 	co = (struct auixp_codec *) hdl;
836 	sc = co->sc;
837 	chain_dma = sc->sc_input_dma;
838 	/* add functions to call back */
839 	chain_dma->intr    = intr;
840 	chain_dma->intrarg = intrarg;
841 
842 	/*
843 	 * Program output DMA chain with blocks from [start...end] with
844 	 * blksize fragments.
845 	 *
846 	 * NOTE, we can assume its in one block since we asked for it to be in
847 	 * one contiguous blob; XXX change this? XXX
848 	 */
849 	blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
850 
851 	/* lookup `start' address in our list of DMA area's */
852 	SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
853 		if (KERNADDR(sound_dma) == start)
854 			break;
855 	}
856 
857 	/* not ours ? then bail out */
858 	if (!sound_dma) {
859 		printf("%s: auixp_trigger_input: bad sound addr %p\n",
860 		    sc->sc_dev.dv_xname, start);
861 		return EINVAL;
862 	}
863 
864 	/* link round-robin daisychain and program hardware */
865 	auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
866 	auixp_program_dma_chain(sc, chain_dma);
867 
868 	/* mark we are now able to run now */
869 	chain_dma->running = 1;
870 
871 	/* update bus-flags; XXX programs more flags XXX */
872 	auixp_update_busbusy(sc);
873 
874 	/* callbacks happen in interrupt routine */
875 	return 0;
876 }
877 
878 
879 /* halt sampling audio, just disable it's dma and update bus state */
880 static int
881 auixp_halt_input(void *hdl)
882 {
883 	struct auixp_codec *co;
884 	struct auixp_softc *sc;
885 	struct auixp_dma   *dma;
886 
887 	co = (struct auixp_codec *) hdl;
888 	sc = co->sc;
889 	dma = sc->sc_input_dma;
890 	auixp_disable_dma(sc, dma);
891 
892 	dma->running = 0;
893 	auixp_update_busbusy(sc);
894 
895 	return 0;
896 }
897 
898 
899 /*
900  * IXP audio interrupt handler
901  *
902  * note that we return the number of bits handled; the return value is not
903  * documentated but i saw it implemented in other drivers. Prolly returning a
904  * value > 0 means "i've dealt with it"
905  *
906  */
907 static int
908 auixp_intr(void *softc)
909 {
910 	struct auixp_softc *sc;
911 	bus_space_tag_t    iot;
912 	bus_space_handle_t ioh;
913 	uint32_t status, enable, detected_codecs;
914 	int ret;
915 
916 	sc = softc;
917 	iot = sc->sc_iot;
918 	ioh = sc->sc_ioh;
919 	ret = 0;
920 	/* get status from the interrupt status register */
921 	status = bus_space_read_4(iot, ioh, ATI_REG_ISR);
922 
923 	if (status == 0)
924 		return 0;
925 
926 	DPRINTF(("%s: (status = %x)\n", sc->sc_dev.dv_xname, status));
927 
928 	/* check DMA UPDATE flags for input & output */
929 	if (status & ATI_REG_ISR_IN_STATUS) {
930 		ret++; DPRINTF(("IN_STATUS\n"));
931 		auixp_dma_update(sc, sc->sc_input_dma);
932 	}
933 	if (status & ATI_REG_ISR_OUT_STATUS) {
934 		ret++; DPRINTF(("OUT_STATUS\n"));
935 		auixp_dma_update(sc, sc->sc_output_dma);
936 	}
937 
938 	/* XXX XRUN flags not used/needed yet; should i implement it? XXX */
939 	/* acknowledge the interrupts nevertheless */
940 	if (status & ATI_REG_ISR_IN_XRUN) {
941 		ret++; DPRINTF(("IN_XRUN\n"));
942 		/* auixp_dma_xrun(sc, sc->sc_input_dma);  */
943 	}
944 	if (status & ATI_REG_ISR_OUT_XRUN) {
945 		ret++; DPRINTF(("OUT_XRUN\n"));
946 		/* auixp_dma_xrun(sc, sc->sc_output_dma); */
947 	}
948 
949 	/* check if we are looking for codec detection */
950 	if (status & CODEC_CHECK_BITS) {
951 		ret++;
952 		/* mark missing codecs as not ready */
953 		detected_codecs = status & CODEC_CHECK_BITS;
954 		sc->sc_codec_not_ready_bits |= detected_codecs;
955 
956 		/* disable detected interupt sources */
957 		enable  = bus_space_read_4(iot, ioh, ATI_REG_IER);
958 		enable &= ~detected_codecs;
959 		bus_space_write_4(iot, ioh, ATI_REG_IER, enable);
960 	}
961 
962 	/* acknowledge interrupt sources */
963 	bus_space_write_4(iot, ioh, ATI_REG_ISR, status);
964 
965 	return ret;
966 }
967 
968 
969 /* allocate memory for dma purposes; on failure of any of the steps, roll back */
970 static int
971 auixp_allocmem(struct auixp_softc *sc, size_t size,
972 	       size_t align, struct auixp_dma *dma)
973 {
974 	int error;
975 
976 	/* remember size */
977 	dma->size = size;
978 
979 	/* allocate DMA safe memory but in just one segment for now :( */
980 	error = bus_dmamem_alloc(sc->sc_dmat, dma->size, align, 0,
981 	    dma->segs, sizeof(dma->segs) / sizeof(dma->segs[0]), &dma->nsegs,
982 	    BUS_DMA_NOWAIT);
983 	if (error)
984 		return error;
985 
986 	/*
987 	 * map allocated memory into kernel virtual address space and keep it
988 	 * coherent with the CPU.
989 	 */
990 	error = bus_dmamem_map(sc->sc_dmat, dma->segs, dma->nsegs, dma->size,
991 				&dma->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
992 	if (error)
993 		goto free;
994 
995 	/* allocate associated dma handle and initialize it. */
996 	error = bus_dmamap_create(sc->sc_dmat, dma->size, 1, dma->size, 0,
997 				  BUS_DMA_NOWAIT, &dma->map);
998 	if (error)
999 		goto unmap;
1000 
1001 	/*
1002 	 * load the dma handle with mappings for a dma transfer; all pages
1003 	 * need to be wired.
1004 	 */
1005 	error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->addr, dma->size, NULL,
1006 				BUS_DMA_NOWAIT);
1007 	if (error)
1008 		goto destroy;
1009 
1010 	return 0;
1011 
1012 destroy:
1013 	bus_dmamap_destroy(sc->sc_dmat, dma->map);
1014 unmap:
1015 	bus_dmamem_unmap(sc->sc_dmat, dma->addr, dma->size);
1016 free:
1017 	bus_dmamem_free(sc->sc_dmat, dma->segs, dma->nsegs);
1018 
1019 	return error;
1020 }
1021 
1022 
1023 /* undo dma mapping and release memory allocated */
1024 static int
1025 auixp_freemem(struct auixp_softc *sc, struct auixp_dma *p)
1026 {
1027 
1028 	bus_dmamap_unload(sc->sc_dmat, p->map);
1029 	bus_dmamap_destroy(sc->sc_dmat, p->map);
1030 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
1031 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1032 
1033 	return 0;
1034 }
1035 
1036 
1037 /* memory map dma memory */
1038 static paddr_t
1039 auixp_mappage(void *hdl, void *mem, off_t off, int prot)
1040 {
1041 	struct auixp_codec *co;
1042 	struct auixp_softc *sc;
1043 	struct auixp_dma *p;
1044 
1045 	co = (struct auixp_codec *) hdl;
1046 	sc  = co->sc;
1047 	/* for sanity */
1048 	if (off < 0)
1049 		return -1;
1050 
1051 	/* look up allocated DMA area */
1052 	SLIST_FOREACH(p, &sc->sc_dma_list, dma_chain) {
1053 		if (KERNADDR(p) == mem)
1054 			break;
1055 	}
1056 
1057 	/* have we found it ? */
1058 	if (!p)
1059 		return -1;
1060 
1061 	/* return mmap'd region */
1062 	return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
1063 			       off, prot, BUS_DMA_WAITOK);
1064 }
1065 
1066 
1067 /*
1068  * Attachment section
1069  */
1070 
1071 /* Is it my hardware? */
1072 static int
1073 auixp_match(struct device *dev, struct cfdata *match, void *aux)
1074 {
1075 	struct pci_attach_args *pa;
1076 
1077 	pa = (struct pci_attach_args *)aux;
1078 	switch(PCI_VENDOR(pa->pa_id)) {
1079 	case PCI_VENDOR_ATI:
1080 		switch(PCI_PRODUCT(pa->pa_id)) {
1081 		case PCI_PRODUCT_ATI_IXP_AUDIO_200:
1082 		case PCI_PRODUCT_ATI_IXP_AUDIO_300:
1083 		case PCI_PRODUCT_ATI_IXP_AUDIO_400:
1084 			return 1;
1085 		}
1086 	}
1087 
1088 	return 0;
1089 }
1090 
1091 
1092 /* it is... now hook up and set up the resources we need */
1093 static void
1094 auixp_attach(struct device *parent, struct device *self, void *aux)
1095 {
1096 	struct auixp_softc *sc;
1097 	struct pci_attach_args *pa;
1098 	pcitag_t tag;
1099 	pci_chipset_tag_t pc;
1100 	pci_intr_handle_t ih;
1101 	const struct auixp_card_type *card;
1102 	const char *intrstr;
1103 	uint32_t data;
1104 	char devinfo[256];
1105 	int revision, len, error;
1106 
1107 	sc = (struct auixp_softc *)self;
1108 	pa = (struct pci_attach_args *)aux;
1109 	tag = pa->pa_tag;
1110 	pc = pa->pa_pc;
1111 #ifdef DEBUG_AUIXP
1112 	static_sc = sc;
1113 #endif
1114 
1115 	/* print information confirming attachment */
1116 	aprint_naive(": Audio controller\n");
1117 
1118 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
1119 	revision = PCI_REVISION(pa->pa_class);
1120 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
1121 
1122 	/* set up details from our set of known `cards'/chips */
1123 	for (card = auixp_card_types; card->pci_vendor_id; card++)
1124 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
1125 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
1126 			sc->type = card->type;
1127 			break;
1128 		}
1129 
1130 	/* device only has 32 bit non prefetchable memory		*/
1131 	/* set MEM space access and enable the card's busmastering	*/
1132 	data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
1133 	data |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
1134 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
1135 
1136 	/* map memory; its not sized -> what is the size? max PCI slot size? */
1137 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_MEM, 0,
1138 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
1139 		aprint_error("%s: can't map memory space\n",
1140 		    sc->sc_dev.dv_xname);
1141 		return;
1142 	}
1143 
1144 	/* Initialize softc */
1145 	sc->sc_tag = tag;
1146 	sc->sc_pct = pc;
1147 	sc->sc_dmat = pa->pa_dmat;
1148 	SLIST_INIT(&sc->sc_dma_list);
1149 
1150 	/* get us the auixp_dma structures */
1151 	auixp_allocate_dma_chain(sc, &sc->sc_output_dma);
1152 	auixp_allocate_dma_chain(sc, &sc->sc_input_dma);
1153 
1154 	/* when that fails we are dead in the water */
1155 	if (!sc->sc_output_dma || !sc->sc_input_dma)
1156 		return;
1157 
1158 	/* fill in the missing details about the dma channels. */
1159 
1160 	/* for output */
1161 	sc->sc_output_dma->linkptr        = ATI_REG_OUT_DMA_LINKPTR;
1162 	sc->sc_output_dma->dma_enable_bit = ATI_REG_CMD_OUT_DMA_EN |
1163 					    ATI_REG_CMD_SEND_EN;
1164 	/* have spdif? then this too! XXX not seeing LED yet! XXX */
1165 	if (sc->has_spdif)
1166 		sc->sc_output_dma->dma_enable_bit |= ATI_REG_CMD_SPDF_OUT_EN;
1167 
1168 	/* and for input */
1169 	sc->sc_input_dma->linkptr         = ATI_REG_IN_DMA_LINKPTR;
1170 	sc->sc_input_dma->dma_enable_bit  = ATI_REG_CMD_IN_DMA_EN  |
1171 					    ATI_REG_CMD_RECEIVE_EN;
1172 
1173 #if 0
1174 	/* could preliminary program DMA chain */
1175 	auixp_program_dma_chain(sc, sc->sc_output_dma);
1176 	auixp_program_dma_chain(sc, sc->sc_input_dma);
1177 #endif
1178 
1179 	/* map interrupt on the pci bus */
1180 	if (pci_intr_map(pa, &ih)) {
1181 		aprint_error("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
1182 		return;
1183 	}
1184 
1185 	/* where are we connected at ? */
1186 	intrstr = pci_intr_string(pc, ih);
1187 
1188 	/* establish interrupt routine hookup at IPL_AUDIO level */
1189 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auixp_intr, self);
1190 	if (sc->sc_ih == NULL) {
1191 		aprint_error("%s: can't establish interrupt",
1192 		    sc->sc_dev.dv_xname);
1193 		if (intrstr != NULL)
1194 			aprint_normal(" at %s", intrstr);
1195 		aprint_normal("\n");
1196 		return;
1197 	}
1198 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
1199 
1200 	/* power up chip */
1201 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
1202 	    pci_activate_null)) && error != EOPNOTSUPP) {
1203 		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
1204 		    error);
1205 		return;
1206 	}
1207 
1208 	/* init chip */
1209 	if (auixp_init(sc) == -1) {
1210 		aprint_error("%s: auixp_attach: unable to initialize the card\n",
1211 		    sc->sc_dev.dv_xname);
1212 		return;
1213 	}
1214 
1215 	/* XXX set up power hooks; not implemented yet XXX */
1216 
1217 	len = 1;	/* shut up gcc */
1218 #ifdef notyet
1219 	/* create suspend save area */
1220 	len = sizeof(uint16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1221 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1222 	sc->savemem = (uint16_t *)malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1223 	if (sc->savemem == NULL) {
1224 		aprint_error("%s: unable to allocate suspend buffer\n",
1225 		    sc->sc_dev.dv_xname);
1226 		return;
1227 	}
1228 
1229 	sc->powerhook = powerhook_establish(auixp_powerhook, sc);
1230 	if (sc->powerhook == NULL)
1231 		aprint_error("%s: WARNING: unable to establish powerhook\n",
1232 		    sc->sc_dev.dv_xname);
1233 
1234 #endif
1235 
1236 	/*
1237 	 * delay further configuration of codecs and audio after interrupts
1238 	 * are enabled.
1239 	 */
1240 	config_interrupts(self, auixp_post_config);
1241 }
1242 
1243 
1244 /* called from autoconfigure system when interrupts are enabled */
1245 static void
1246 auixp_post_config(struct device *self)
1247 {
1248 	struct auixp_softc *sc;
1249 	struct auixp_codec *codec;
1250 	int codec_nr;
1251 	int res, i;
1252 
1253 	sc = (struct auixp_softc *)self;
1254 	/* detect the AC97 codecs */
1255 	auixp_autodetect_codecs(sc);
1256 
1257 	/* setup audio translation formats : following codec0 (!) */
1258 	codec = &sc->sc_codec[0];
1259 	if (!codec->present) {
1260 		/* nothing??? then invalidate all formats */
1261 		for (i = 0; i < AUIXP_NFORMATS; i++) {
1262 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
1263 		}
1264 		return;
1265 	}
1266 
1267 	/* copy formats and invalidate entries not suitable for codec0 */
1268 	memcpy(sc->sc_formats, auixp_formats, sizeof(auixp_formats));
1269 	sc->has_4ch   = AC97_IS_4CH(codec->codec_if);
1270 	sc->has_6ch   = AC97_IS_6CH(codec->codec_if);
1271 	sc->is_fixed  = AC97_IS_FIXED_RATE(codec->codec_if);
1272 	sc->has_spdif = AC97_HAS_SPDIF(codec->codec_if);
1273 
1274 	for (i = 0; i < AUIXP_NFORMATS; i++) {
1275 		if (sc->is_fixed) {
1276 			sc->sc_formats[i].frequency_type = 1;
1277 			sc->sc_formats[i].frequency[0]   = 48000;
1278 		}
1279 		switch (sc->sc_formats[i].channels) {
1280 		case 4 :
1281 			if (sc->has_4ch)
1282 				break;
1283 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
1284 			break;
1285 		case 6 :
1286 			if (sc->has_6ch)
1287 				break;
1288 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
1289 			break;
1290 		default :
1291 			break;
1292 		}
1293 	}
1294 
1295 	/*
1296 	 * Create all encodings (and/or -translations) based on the formats
1297 	 * supported. */
1298 	res = auconv_create_encodings(sc->sc_formats, AUIXP_NFORMATS,
1299 	    &sc->sc_encodings);
1300 	if (res) {
1301 		printf("%s: auconv_create_encodings failed; "
1302 		    "no attachments\n", sc->sc_dev.dv_xname);
1303 		return;
1304 	}
1305 
1306 	/* attach audio devices for all detected codecs */
1307 	/* XXX wise? look at other multiple-codec able chipsets XXX */
1308 	for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1309 		codec = &sc->sc_codec[codec_nr];
1310 		if (codec->present)
1311 			audio_attach_mi(&auixp_hw_if, codec, &sc->sc_dev);
1312 	}
1313 
1314 	/* done! now enable all interrupts we can service */
1315 	auixp_enable_interrupts(sc);
1316 }
1317 
1318 
1319 static void
1320 auixp_enable_interrupts(struct auixp_softc *sc)
1321 {
1322 	bus_space_tag_t     iot;
1323 	bus_space_handle_t  ioh;
1324 	uint32_t value;
1325 
1326 	iot = sc->sc_iot;
1327 	ioh = sc->sc_ioh;
1328 	/* clear all pending */
1329 	bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1330 
1331 	/* enable all relevant interrupt sources we can handle */
1332 	value = bus_space_read_4(iot, ioh, ATI_REG_IER);
1333 
1334 	value |= ATI_REG_IER_IO_STATUS_EN;
1335 #ifdef notyet
1336 	value |= ATI_REG_IER_IN_XRUN_EN;
1337 	value |= ATI_REG_IER_OUT_XRUN_EN;
1338 
1339 	value |= ATI_REG_IER_SPDIF_XRUN_EN;
1340 	value |= ATI_REG_IER_SPDF_STATUS_EN;
1341 #endif
1342 
1343 	bus_space_write_4(iot, ioh, ATI_REG_IER, value);
1344 }
1345 
1346 
1347 static void
1348 auixp_disable_interrupts(struct auixp_softc *sc)
1349 {
1350 	bus_space_tag_t     iot;
1351 	bus_space_handle_t  ioh;
1352 
1353 	iot = sc->sc_iot;
1354 	ioh = sc->sc_ioh;
1355 	/* disable all interrupt sources */
1356 	bus_space_write_4(iot, ioh, ATI_REG_IER, 0);
1357 
1358 	/* clear all pending */
1359 	bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1360 }
1361 
1362 
1363 /* dismantle what we've set up by undoing setup */
1364 static int
1365 auixp_detach(struct device *self, int flags)
1366 {
1367 	struct auixp_softc *sc;
1368 
1369 	sc = (struct auixp_softc *)self;
1370 	/* XXX shouldn't we just reset the chip? XXX */
1371 	/*
1372 	 * should we explicitly disable interrupt generation and acknowledge
1373 	 * what's left on? better be safe than sorry.
1374 	 */
1375 	auixp_disable_interrupts(sc);
1376 
1377 	/* tear down .... */
1378 	config_detach(&sc->sc_dev, flags);	/* XXX OK? XXX */
1379 
1380 	if (sc->sc_ih != NULL)
1381 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1382 	if (sc->sc_ios)
1383 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1384 
1385 	if (sc->savemem)
1386 		free(sc->savemem, M_DEVBUF);
1387 
1388 	return 0;
1389 }
1390 
1391 
1392 /*
1393  * codec handling
1394  *
1395  * IXP audio support can have upto 3 codecs! are they chained ? or
1396  * alternative outlets with the same audio feed i.e. with different mixer
1397  * settings? XXX does NetBSD support more than one audio codec? XXX
1398  */
1399 
1400 
1401 static int
1402 auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1403 {
1404 	struct auixp_codec *ixp_codec;
1405 
1406 	ixp_codec = aux;
1407 	ixp_codec->codec_if = codec_if;
1408 	ixp_codec->present  = 1;
1409 
1410 	return 0;
1411 }
1412 
1413 
1414 static int
1415 auixp_read_codec(void *aux, uint8_t reg, uint16_t *result)
1416 {
1417 	struct auixp_codec *co;
1418 	struct auixp_softc *sc;
1419 	bus_space_tag_t     iot;
1420 	bus_space_handle_t  ioh;
1421 	uint32_t data;
1422 	int timeout;
1423 
1424 	co  = aux;
1425 	sc  = co->sc;
1426 	iot = sc->sc_iot;
1427 	ioh = sc->sc_ioh;
1428 	if (auixp_wait_for_codecs(sc, "read_codec"))
1429 		return 0xffff;
1430 
1431 	/* build up command for reading codec register */
1432 	data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1433 		ATI_REG_PHYS_OUT_ADDR_EN |
1434 		ATI_REG_PHYS_OUT_RW |
1435 		co->codec_nr;
1436 
1437 	bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data);
1438 
1439 	if (auixp_wait_for_codecs(sc, "read_codec"))
1440 		return 0xffff;
1441 
1442 	/* wait until codec info is clocked in */
1443 	timeout = 500;		/* 500*2 usec -> 0.001 sec */
1444 	do {
1445 		data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR);
1446 		if (data & ATI_REG_PHYS_IN_READ_FLAG) {
1447 			DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n",
1448 				reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT)
1449 			);
1450 			*result = data >> ATI_REG_PHYS_IN_DATA_SHIFT;
1451 			return 0;
1452 		}
1453 		DELAY(2);
1454 		timeout--;
1455 	} while (timeout > 0);
1456 
1457 	if (reg < 0x7c)
1458 		printf("%s: codec read timeout! (reg %x)\n",
1459 		    sc->sc_dev.dv_xname, reg);
1460 
1461 	return 0xffff;
1462 }
1463 
1464 
1465 static int
1466 auixp_write_codec(void *aux, uint8_t reg, uint16_t data)
1467 {
1468 	struct auixp_codec *co;
1469 	struct auixp_softc *sc;
1470 	bus_space_tag_t     iot;
1471 	bus_space_handle_t  ioh;
1472 	uint32_t value;
1473 
1474 	DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data));
1475 	co  = aux;
1476 	sc  = co->sc;
1477 	iot = sc->sc_iot;
1478 	ioh = sc->sc_ioh;
1479 	if (auixp_wait_for_codecs(sc, "write_codec"))
1480 		return -1;
1481 
1482 	/* build up command for writing codec register */
1483 	value = (((uint32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) |
1484 		(((uint32_t)  reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1485 		ATI_REG_PHYS_OUT_ADDR_EN |
1486 		co->codec_nr;
1487 
1488 	bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value);
1489 
1490 	return 0;
1491 }
1492 
1493 
1494 static int
1495 auixp_reset_codec(void *aux)
1496 {
1497 
1498 	/* nothing to be done? */
1499 	return 0;
1500 }
1501 
1502 
1503 static enum ac97_host_flags
1504 auixp_flags_codec(void *aux)
1505 {
1506 	struct auixp_codec *ixp_codec;
1507 
1508 	ixp_codec = aux;
1509 	return ixp_codec->codec_flags;
1510 }
1511 
1512 
1513 static int
1514 auixp_wait_for_codecs(struct auixp_softc *sc, const char *func)
1515 {
1516 	bus_space_tag_t      iot;
1517 	bus_space_handle_t   ioh;
1518 	uint32_t value;
1519 	int timeout;
1520 
1521 	iot = sc->sc_iot;
1522 	ioh = sc->sc_ioh;
1523 	/* wait until all codec transfers are done */
1524 	timeout = 500;		/* 500*2 usec -> 0.001 sec */
1525 	do {
1526 		value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR);
1527 		if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0)
1528 			return 0;
1529 
1530 		DELAY(2);
1531 		timeout--;
1532 	} while (timeout > 0);
1533 
1534 	printf("%s: %s: timed out\n", func, sc->sc_dev.dv_xname);
1535 	return -1;
1536 }
1537 
1538 
1539 
1540 static void
1541 auixp_autodetect_codecs(struct auixp_softc *sc)
1542 {
1543 	bus_space_tag_t      iot;
1544 	bus_space_handle_t   ioh;
1545 	struct auixp_codec  *codec;
1546 	int timeout, codec_nr;
1547 
1548 	iot = sc->sc_iot;
1549 	ioh = sc->sc_ioh;
1550 	/* ATI IXP can have upto 3 codecs; mark all codecs as not existing */
1551 	sc->sc_codec_not_ready_bits = 0;
1552 	sc->sc_num_codecs = 0;
1553 
1554 	/* enable all codecs to interrupt as well as the new frame interrupt */
1555 	bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS);
1556 
1557 	/* wait for the interrupts to happen */
1558 	timeout = 100;		/* 100.000 usec -> 0.1 sec */
1559 
1560 	while (timeout > 0) {
1561 		DELAY(1000);
1562 		if (sc->sc_codec_not_ready_bits)
1563 			break;
1564 		timeout--;
1565 	}
1566 
1567 	if (timeout == 0)
1568 		printf("%s: WARNING: timeout during codec detection; "
1569 			"codecs might be present but haven't interrupted\n",
1570 			sc->sc_dev.dv_xname);
1571 
1572 	/* disable all interrupts for now */
1573 	auixp_disable_interrupts(sc);
1574 
1575 	/* Attach AC97 host interfaces */
1576 	for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1577 		codec = &sc->sc_codec[codec_nr];
1578 		bzero(codec, sizeof(struct auixp_codec));
1579 
1580 		codec->sc       = sc;
1581 		codec->codec_nr = codec_nr;
1582 		codec->present  = 0;
1583 
1584 		codec->host_if.arg    = codec;
1585 		codec->host_if.attach = auixp_attach_codec;
1586 		codec->host_if.read   = auixp_read_codec;
1587 		codec->host_if.write  = auixp_write_codec;
1588 		codec->host_if.reset  = auixp_reset_codec;
1589 		codec->host_if.flags  = auixp_flags_codec;
1590 	}
1591 
1592 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) {
1593 		/* codec 0 present */
1594 		DPRINTF(("auixp : YAY! codec 0 present!\n"));
1595 		if (ac97_attach(&sc->sc_codec[0].host_if, &sc->sc_dev) == 0)
1596 			sc->sc_num_codecs++;
1597 	}
1598 
1599 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) {
1600 		/* codec 1 present */
1601 		DPRINTF(("auixp : YAY! codec 1 present!\n"));
1602 		if (ac97_attach(&sc->sc_codec[1].host_if, &sc->sc_dev) == 0)
1603 			sc->sc_num_codecs++;
1604 	}
1605 
1606 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) {
1607 		/* codec 2 present */
1608 		DPRINTF(("auixp : YAY! codec 2 present!\n"));
1609 		if (ac97_attach(&sc->sc_codec[2].host_if, &sc->sc_dev) == 0)
1610 			sc->sc_num_codecs++;
1611 	}
1612 
1613 	if (sc->sc_num_codecs == 0) {
1614 		printf("%s: no codecs detected or "
1615 				"no codecs managed to initialise\n",
1616 				sc->sc_dev.dv_xname);
1617 		return;
1618 	}
1619 
1620 }
1621 
1622 
1623 
1624 /* initialisation routines */
1625 
1626 static void
1627 auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1628 {
1629 	bus_space_tag_t      iot;
1630 	bus_space_handle_t   ioh;
1631 	uint32_t value;
1632 
1633 	iot = sc->sc_iot;
1634 	ioh = sc->sc_ioh;
1635 	/* lets not stress the DMA engine more than nessisary */
1636 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1637 	if (value & dma->dma_enable_bit) {
1638 		value &= ~dma->dma_enable_bit;
1639 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1640 	}
1641 }
1642 
1643 
1644 static void
1645 auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1646 {
1647 	bus_space_tag_t      iot;
1648 	bus_space_handle_t   ioh;
1649 	uint32_t value;
1650 
1651 	iot = sc->sc_iot;
1652 	ioh = sc->sc_ioh;
1653 	/* lets not stress the DMA engine more than nessisary */
1654 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1655 	if (!(value & dma->dma_enable_bit)) {
1656 		value |= dma->dma_enable_bit;
1657 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1658 	}
1659 }
1660 
1661 
1662 static void
1663 auixp_reset_aclink(struct auixp_softc *sc)
1664 {
1665 	bus_space_tag_t      iot;
1666 	bus_space_handle_t   ioh;
1667 	uint32_t value, timeout;
1668 
1669 	iot = sc->sc_iot;
1670 	ioh = sc->sc_ioh;
1671 
1672 	/* if power is down, power it up */
1673 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1674 	if (value & ATI_REG_CMD_POWERDOWN) {
1675 		printf("%s: powering up\n", sc->sc_dev.dv_xname);
1676 
1677 		/* explicitly enable power */
1678 		value &= ~ATI_REG_CMD_POWERDOWN;
1679 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1680 
1681 		/* have to wait at least 10 usec for it to initialise */
1682 		DELAY(20);
1683 	};
1684 
1685 	printf("%s: soft resetting aclink\n", sc->sc_dev.dv_xname);
1686 
1687 	/* perform a soft reset */
1688 	value  = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1689 	value |= ATI_REG_CMD_AC_SOFT_RESET;
1690 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1691 
1692 	/* need to read the CMD reg and wait aprox. 10 usec to init */
1693 	value  = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1694 	DELAY(20);
1695 
1696 	/* clear soft reset flag again */
1697 	value  = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1698 	value &= ~ATI_REG_CMD_AC_SOFT_RESET;
1699 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1700 
1701 	/* check if the ac-link is working; reset device otherwise */
1702 	timeout = 10;
1703 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1704 	while (!(value & ATI_REG_CMD_ACLINK_ACTIVE)) {
1705 		printf("%s: not up; resetting aclink hardware\n",
1706 				sc->sc_dev.dv_xname);
1707 
1708 		/* dip aclink reset but keep the acsync */
1709 		value &= ~ATI_REG_CMD_AC_RESET;
1710 		value |=  ATI_REG_CMD_AC_SYNC;
1711 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1712 
1713 		/* need to read CMD again and wait again (clocking in issue?) */
1714 		value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1715 		DELAY(20);
1716 
1717 		/* assert aclink reset again */
1718 		value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1719 		value |=  ATI_REG_CMD_AC_RESET;
1720 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1721 
1722 		/* check if its active now */
1723 		value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1724 
1725 		timeout--;
1726 		if (timeout == 0) break;
1727 	};
1728 
1729 	if (timeout == 0) {
1730 		printf("%s: giving up aclink reset\n", sc->sc_dev.dv_xname);
1731 	};
1732 	if (timeout != 10) {
1733 		printf("%s: aclink hardware reset successful\n",
1734 			sc->sc_dev.dv_xname);
1735 	};
1736 
1737 	/* assert reset and sync for safety */
1738 	value  = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1739 	value |= ATI_REG_CMD_AC_SYNC | ATI_REG_CMD_AC_RESET;
1740 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1741 }
1742 
1743 
1744 /* chip hard init */
1745 static int
1746 auixp_init(struct auixp_softc *sc)
1747 {
1748 	bus_space_tag_t      iot;
1749 	bus_space_handle_t   ioh;
1750 	uint32_t value;
1751 
1752 	iot = sc->sc_iot;
1753 	ioh = sc->sc_ioh;
1754 	/* disable all interrupts and clear all sources */
1755 	auixp_disable_interrupts(sc);
1756 
1757 	/* clear all DMA enables (preserving rest of settings) */
1758 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1759 	value &= ~( ATI_REG_CMD_IN_DMA_EN  |
1760 		    ATI_REG_CMD_OUT_DMA_EN |
1761 		    ATI_REG_CMD_SPDF_OUT_EN );
1762 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1763 
1764 	/* Reset AC-link */
1765 	auixp_reset_aclink(sc);
1766 
1767 	/*
1768 	 * codecs get auto-detected later
1769 	 *
1770 	 * note: we are NOT enabling interrupts yet, no codecs have been
1771 	 * detected yet nor is anything else set up
1772 	 */
1773 
1774 	return 0;
1775 }
1776 
1777 
1778 /*
1779  * TODO power saving and suspend / resume support
1780  *
1781  */
1782 
1783 #if 0
1784 static void
1785 auixp_powerhook(int why, void *hdl)
1786 {
1787 	struct auixp_softc *sc;
1788 
1789 	sc = (struct auixp_softc *)hdl;
1790 	switch (why) {
1791 	case PWR_SUSPEND:
1792 	case PWR_STANDBY:
1793 		auixp_suspend(sc);
1794 		break;
1795 	case PWR_RESUME:
1796 		auixp_resume(sc);
1797 /* XXX fix me XXX */
1798 //		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1799 		break;
1800 	}
1801 }
1802 
1803 
1804 static int
1805 auixp_suspend(struct auixp_softc *sc)
1806 {
1807 
1808 	/* XXX no power functions yet XXX */
1809 	return 0;
1810 }
1811 
1812 
1813 static int
1814 auixp_resume(struct auixp_softc *sc)
1815 {
1816 
1817 	/* XXX no power functions yet XXX */
1818 	return 0;
1819 }
1820 #endif /* 0 */
1821 
1822 #ifdef DEBUG_AUIXP
1823 
1824 static void
1825 auixp_dumpreg(void)
1826 {
1827 	struct auixp_softc  *sc;
1828 	bus_space_tag_t      iot;
1829 	bus_space_handle_t   ioh;
1830 	int i;
1831 
1832 	sc  = static_sc;
1833 	iot = sc->sc_iot;
1834 	ioh = sc->sc_ioh;
1835 	printf("%s register dump:\n", sc->sc_dev.dv_xname);
1836 	for (i = 0; i < 256; i+=4) {
1837 		printf("\t0x%02x: 0x%08x\n", i, bus_space_read_4(iot, ioh, i));
1838 	}
1839 	printf("\n");
1840 }
1841 #endif
1842