auixp.c revision 1.11 1 /* $NetBSD: auixp.c,v 1.11 2006/01/24 23:29:05 reinoud Exp $ */
2
3 /*
4 * Copyright (c) 2004, 2005 Reinoud Zandijk <reinoud (at) 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.11 2006/01/24 23:29:05 reinoud 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 static int auixp_power(struct auixp_softc *, int);
170 #if 0
171 static void auixp_powerhook(int, void *);
172 static int auixp_suspend(struct auixp_softc *);
173 static int auixp_resume(struct auixp_softc *);
174 #endif
175
176
177 /* Supporting subroutines */
178 static int auixp_init(struct auixp_softc *);
179 static void auixp_autodetect_codecs(struct auixp_softc *);
180 static void auixp_post_config(struct device *);
181
182 static void auixp_reset_aclink(struct auixp_softc *);
183 static int auixp_attach_codec(void *, struct ac97_codec_if *);
184 static int auixp_read_codec(void *, uint8_t, uint16_t *);
185 static int auixp_write_codec(void *, uint8_t, uint16_t);
186 static int auixp_wait_for_codecs(struct auixp_softc *, const char *);
187 static int auixp_reset_codec(void *);
188 static enum ac97_host_flags auixp_flags_codec(void *);
189
190 static void auixp_enable_dma(struct auixp_softc *, struct auixp_dma *);
191 static void auixp_disable_dma(struct auixp_softc *, struct auixp_dma *);
192 static void auixp_enable_interrupts(struct auixp_softc *);
193 static void auixp_disable_interrupts(struct auixp_softc *);
194
195
196 /* statics */
197 static void auixp_link_daisychain(struct auixp_softc *,
198 struct auixp_dma *, struct auixp_dma *,
199 int, int);
200 static int auixp_allocate_dma_chain(struct auixp_softc *,
201 struct auixp_dma **);
202 static void auixp_program_dma_chain(struct auixp_softc *,
203 struct auixp_dma *);
204 static void auixp_dma_update(struct auixp_softc *, struct auixp_dma *);
205 static void auixp_update_busbusy(struct auixp_softc *);
206
207
208 #ifdef DEBUG_AUIXP
209 static struct auixp_softc *static_sc;
210 sdtatic void auixp_dumpreg(void);
211 # define DPRINTF(x) printf x;
212 #else
213 # define DPRINTF(x)
214 #endif
215
216
217 static const struct audio_hw_if auixp_hw_if = {
218 NULL, /* open */
219 NULL, /* close */
220 NULL, /* drain */
221 auixp_query_encoding,
222 auixp_set_params,
223 auixp_round_blocksize,
224 auixp_commit_settings,
225 NULL, /* init_output */
226 NULL, /* init_input */
227 NULL, /* start_output */
228 NULL, /* start_input */
229 auixp_halt_output,
230 auixp_halt_input,
231 NULL, /* speaker_ctl */
232 auixp_getdev,
233 NULL, /* getfd */
234 auixp_set_port,
235 auixp_get_port,
236 auixp_query_devinfo,
237 auixp_malloc,
238 auixp_free,
239 auixp_round_buffersize,
240 auixp_mappage,
241 auixp_get_props,
242 auixp_trigger_output,
243 auixp_trigger_input
244 };
245
246
247 CFATTACH_DECL(auixp, sizeof(struct auixp_softc), auixp_match, auixp_attach,
248 auixp_detach, NULL);
249
250
251 /*
252 * audio(9) functions
253 */
254
255 static int
256 auixp_query_encoding(void *hdl, struct audio_encoding *ae)
257 {
258 struct auixp_codec *co;
259 struct auixp_softc *sc;
260
261 co = (struct auixp_codec *) hdl;
262 sc = co->sc;
263 return auconv_query_encoding(sc->sc_encodings, ae);
264 }
265
266
267 static int
268 auixp_set_rate(struct auixp_codec *co, int mode, u_int srate)
269 {
270 int ret;
271 u_int ratetmp;
272
273 ratetmp = srate;
274 if (mode == AUMODE_RECORD) {
275 ret = co->codec_if->vtbl->set_rate(co->codec_if,
276 AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
277 return ret;
278 }
279
280 /* play mode */
281 ret = co->codec_if->vtbl->set_rate(co->codec_if,
282 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
283 if (ret)
284 return ret;
285
286 ratetmp = srate;
287 ret = co->codec_if->vtbl->set_rate(co->codec_if,
288 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
289 if (ret)
290 return ret;
291
292 ratetmp = srate;
293 ret = co->codec_if->vtbl->set_rate(co->codec_if,
294 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
295 return ret;
296 }
297
298
299 /* commit setting and program ATI IXP chip */
300 static int
301 auixp_commit_settings(void *hdl)
302 {
303 struct auixp_codec *co;
304 struct auixp_softc *sc;
305 bus_space_tag_t iot;
306 bus_space_handle_t ioh;
307 struct audio_params *params;
308 uint32_t value;
309
310 /* XXX would it be better to stop interrupts first? XXX */
311 co = (struct auixp_codec *) hdl;
312 sc = co->sc;
313 iot = sc->sc_iot;
314 ioh = sc->sc_ioh;
315
316 /* process input settings */
317 params = &sc->sc_play_params;
318
319 /* set input interleaving (precision) */
320 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
321 value &= ~ATI_REG_CMD_INTERLEAVE_IN;
322 if (params->precision <= 16)
323 value |= ATI_REG_CMD_INTERLEAVE_IN;
324 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
325
326 /* process output settings */
327 params = &sc->sc_play_params;
328
329 value = bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT);
330 value &= ~ATI_REG_OUT_DMA_SLOT_MASK;
331
332 /* TODO SPDIF case for 8 channels */
333 switch (params->channels) {
334 case 6:
335 value |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
336 ATI_REG_OUT_DMA_SLOT_BIT(8);
337 /* fallthru */
338 case 4:
339 value |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
340 ATI_REG_OUT_DMA_SLOT_BIT(9);
341 /* fallthru */
342 default:
343 value |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
344 ATI_REG_OUT_DMA_SLOT_BIT(4);
345 break;
346 }
347 /* set output threshold */
348 value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
349 bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value);
350
351 /* set output interleaving (precision) */
352 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
353 value &= ~ATI_REG_CMD_INTERLEAVE_OUT;
354 if (params->precision <= 16)
355 value |= ATI_REG_CMD_INTERLEAVE_OUT;
356 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
357
358 /* enable 6 channel reordering */
359 value = bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER);
360 value &= ~ATI_REG_6CH_REORDER_EN;
361 if (params->channels == 6)
362 value |= ATI_REG_6CH_REORDER_EN;
363 bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value);
364
365 if (sc->has_spdif) {
366 /* set SPDIF (if present) */
367 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
368 value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK;
369 value |= ATI_REG_CMD_SPDF_CONFIG_34; /* NetBSD AC'97 default */
370
371 /* XXX this prolly is not nessisary unless splitted XXX */
372 value &= ~ATI_REG_CMD_INTERLEAVE_SPDF;
373 if (params->precision <= 16)
374 value |= ATI_REG_CMD_INTERLEAVE_SPDF;
375 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
376 }
377
378 return 0;
379 }
380
381
382 /* set audio properties in desired setting */
383 static int
384 auixp_set_params(void *hdl, int setmode, int usemode, audio_params_t *play,
385 audio_params_t *rec, stream_filter_list_t *pfil,
386 stream_filter_list_t *rfil)
387 {
388 struct auixp_codec *co;
389 struct auixp_softc *sc;
390 audio_params_t *params;
391 stream_filter_list_t *fil;
392 int mode, index;
393
394 /*
395 * In current NetBSD AC'97 implementation, SPDF is linked to channel 3
396 * and 4 i.e. stereo output.
397 */
398
399 co = (struct auixp_codec *) hdl;
400 sc = co->sc;
401 for (mode = AUMODE_RECORD; mode != -1;
402 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
403 if ((setmode & mode) == 0)
404 continue;
405
406 params = (mode == AUMODE_PLAY) ? play : rec;
407 fil = (mode == AUMODE_PLAY) ? pfil : rfil;
408 if (params == NULL)
409 continue;
410
411 /* AD1888 settings ... don't know the IXP limits */
412 if (params->sample_rate < AUIXP_MINRATE)
413 return EINVAL;
414 if (params->sample_rate > AUIXP_MAXRATE)
415 return EINVAL;
416
417 index = auconv_set_converter(sc->sc_formats, AUIXP_NFORMATS,
418 mode, params, TRUE, fil);
419
420 /* nothing found? */
421 if (index < 0)
422 return EINVAL;
423
424 /* not sure yet as to why i have to change params here */
425 if (fil->req_size > 0)
426 params = &fil->filters[0].param;
427
428 /* if variable speed and we can't set the desired rate, fail */
429 if ((sc->sc_formats[index].frequency_type != 1) &&
430 auixp_set_rate(co, mode, params->sample_rate))
431 return EINVAL;
432
433 /* preserve the settings */
434 if (mode == AUMODE_PLAY)
435 sc->sc_play_params = *params;
436 if (mode == AUMODE_RECORD)
437 sc->sc_rec_params = *params;
438 }
439
440 return 0;
441 }
442
443
444 /* called to translate a requested blocksize to a hw-possible one */
445 static int
446 auixp_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
447 {
448 uint32_t new_bs;
449
450 new_bs = bs;
451 /* Be conservative; align to 32 bytes and maximise it to 64 kb */
452 /* 256 kb possible */
453 if (new_bs > 0x10000)
454 bs = 0x10000; /* 64 kb max */
455 new_bs = (bs & ~0x20); /* 32 bytes align */
456
457 return new_bs;
458 }
459
460
461 /*
462 * allocate dma capable memory and record its information for later retrieval
463 * when we program the dma chain itself. The trigger routines passes on the
464 * kernel virtual address we return here as a reference to the mapping.
465 */
466 static void *
467 auixp_malloc(void *hdl, int direction, size_t size,
468 struct malloc_type *type, int flags)
469 {
470 struct auixp_codec *co;
471 struct auixp_softc *sc;
472 struct auixp_dma *dma;
473 int error;
474
475 co = (struct auixp_codec *) hdl;
476 sc = co->sc;
477 /* get us a auixp_dma structure */
478 dma = malloc(sizeof(*dma), type, flags);
479 if (!dma)
480 return NULL;
481
482 /* get us a dma buffer itself */
483 error = auixp_allocmem(sc, size, 16, dma);
484 if (error) {
485 free(dma, type);
486 printf("%s: auixp_malloc: not enough memory\n",
487 sc->sc_dev.dv_xname);
488
489 return NULL;
490 }
491 SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain);
492
493 DPRINTF(("auixp_malloc: returning kern %p, hw 0x%08x for %d bytes "
494 "in %d segs\n", KERNADDR(dma), (uint32_t) DMAADDR(dma), dma->size,
495 dma->nsegs)
496 );
497
498 return KERNADDR(dma);
499 }
500
501
502 /*
503 * free and release dma capable memory we allocated before and remove its
504 * recording
505 */
506 static void
507 auixp_free(void *hdl, void *addr, struct malloc_type *type)
508 {
509 struct auixp_codec *co;
510 struct auixp_softc *sc;
511 struct auixp_dma *dma;
512
513 co = (struct auixp_codec *) hdl;
514 sc = co->sc;
515 SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) {
516 if (KERNADDR(dma) == addr) {
517 SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma,
518 dma_chain);
519 auixp_freemem(sc, dma);
520 free(dma, type);
521 return;
522 }
523 }
524 }
525
526
527 static int
528 auixp_getdev(void *hdl, struct audio_device *ret)
529 {
530
531 *ret = auixp_device;
532 return 0;
533 }
534
535
536 /* pass request to AC'97 codec code */
537 static int
538 auixp_set_port(void *hdl, mixer_ctrl_t *mc)
539 {
540 struct auixp_codec *co;
541
542 co = (struct auixp_codec *) hdl;
543 return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc);
544 }
545
546
547 /* pass request to AC'97 codec code */
548 static int
549 auixp_get_port(void *hdl, mixer_ctrl_t *mc)
550 {
551 struct auixp_codec *co;
552
553 co = (struct auixp_codec *) hdl;
554 return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc);
555 }
556
557 /* pass request to AC'97 codec code */
558 static int
559 auixp_query_devinfo(void *hdl, mixer_devinfo_t *di)
560 {
561 struct auixp_codec *co;
562
563 co = (struct auixp_codec *) hdl;
564 return co->codec_if->vtbl->query_devinfo(co->codec_if, di);
565 }
566
567
568 static size_t
569 auixp_round_buffersize(void *hdl, int direction, size_t bufsize)
570 {
571
572 /* XXX force maximum? i.e. 256 kb? */
573 return bufsize;
574 }
575
576
577 static int
578 auixp_get_props(void *hdl)
579 {
580
581 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
582 }
583
584
585 /*
586 * A dma descriptor has dma->nsegs segments defined in dma->segs set up when
587 * we claimed the memory.
588 *
589 * Due to our demand for one contiguous DMA area, we only have one segment. A
590 * c_dma structure is about 3 kb for the 256 entries we maximally program
591 * -arbitrary limit AFAIK- so all is most likely to be in one segment/page
592 * anyway.
593 *
594 * XXX ought to implement fragmented dma area XXX
595 *
596 * Note that _v variables depict kernel virtual addresses, _p variables depict
597 * physical addresses.
598 */
599 static void
600 auixp_link_daisychain(struct auixp_softc *sc,
601 struct auixp_dma *c_dma, struct auixp_dma *s_dma,
602 int blksize, int blocks)
603 {
604 atiixp_dma_desc_t *caddr_v, *next_caddr_v;
605 uint32_t caddr_p, next_caddr_p, saddr_p;
606 int i;
607
608 /* just make sure we are not changing when its running */
609 auixp_disable_dma(sc, c_dma);
610
611 /* setup dma chain start addresses */
612 caddr_v = KERNADDR(c_dma);
613 caddr_p = DMAADDR(c_dma);
614 saddr_p = DMAADDR(s_dma);
615
616 /* program the requested number of blocks */
617 for (i = 0; i < blocks; i++) {
618 /* clear the block just in case */
619 bzero(caddr_v, sizeof(atiixp_dma_desc_t));
620
621 /* round robin the chain dma addresses for its successor */
622 next_caddr_v = caddr_v + 1;
623 next_caddr_p = caddr_p + sizeof(atiixp_dma_desc_t);
624
625 if (i == blocks-1) {
626 next_caddr_v = KERNADDR(c_dma);
627 next_caddr_p = DMAADDR(c_dma);
628 }
629
630 /* fill in the hardware dma chain descriptor in little-endian */
631 caddr_v->addr = htole32(saddr_p);
632 caddr_v->status = htole16(0);
633 caddr_v->size = htole16((blksize >> 2)); /* in dwords (!!!) */
634 caddr_v->next = htole32(next_caddr_p);
635
636 /* advance slot */
637 saddr_p += blksize; /* XXX assuming contiguous XXX */
638 caddr_v = next_caddr_v;
639 caddr_p = next_caddr_p;
640 }
641 }
642
643
644 static int
645 auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap)
646 {
647 struct auixp_dma *dma;
648 int error;
649
650 /* allocate keeper of dma area */
651 *dmap = NULL;
652 dma = malloc(sizeof(struct auixp_dma), M_DEVBUF, M_NOWAIT | M_ZERO);
653 if (!dma)
654 return ENOMEM;
655
656 /* allocate for daisychain of IXP hardware-dma descriptors */
657 error = auixp_allocmem(sc, DMA_DESC_CHAIN * sizeof(atiixp_dma_desc_t),
658 16, dma);
659 if (error) {
660 printf("%s: can't malloc dma descriptor chain\n",
661 sc->sc_dev.dv_xname);
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;
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 auixp_power(sc, PCI_PMCSR_STATE_D0);
1202
1203 /* init chip */
1204 if (auixp_init(sc) == -1) {
1205 aprint_error("%s: auixp_attach: unable to initialize the card\n",
1206 sc->sc_dev.dv_xname);
1207 return;
1208 }
1209
1210 /* XXX set up power hooks; not implemented yet XXX */
1211
1212 len = 1; /* shut up gcc */
1213 #ifdef notyet
1214 /* create suspend save area */
1215 len = sizeof(uint16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1216 + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1217 sc->savemem = (uint16_t *)malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1218 if (sc->savemem == NULL) {
1219 aprint_error("%s: unable to allocate suspend buffer\n",
1220 sc->sc_dev.dv_xname);
1221 return;
1222 }
1223
1224 sc->powerhook = powerhook_establish(auixp_powerhook, sc);
1225 if (sc->powerhook == NULL)
1226 aprint_error("%s: WARNING: unable to establish powerhook\n",
1227 sc->sc_dev.dv_xname);
1228
1229 #endif
1230
1231 /*
1232 * delay further configuration of codecs and audio after interrupts
1233 * are enabled.
1234 */
1235 config_interrupts(self, auixp_post_config);
1236 }
1237
1238
1239 /* called from autoconfigure system when interrupts are enabled */
1240 static void
1241 auixp_post_config(struct device *self)
1242 {
1243 struct auixp_softc *sc;
1244 struct auixp_codec *codec;
1245 int codec_nr;
1246 int res, i;
1247
1248 sc = (struct auixp_softc *)self;
1249 /* detect the AC97 codecs */
1250 auixp_autodetect_codecs(sc);
1251
1252 /* setup audio translation formats : following codec0 (!) */
1253 codec = &sc->sc_codec[0];
1254 if (!codec->present) {
1255 /* nothing??? then invalidate all formats */
1256 for (i = 0; i < AUIXP_NFORMATS; i++) {
1257 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1258 }
1259 return;
1260 }
1261
1262 /* copy formats and invalidate entries not suitable for codec0 */
1263 memcpy(sc->sc_formats, auixp_formats, sizeof(auixp_formats));
1264 sc->has_4ch = AC97_IS_4CH(codec->codec_if);
1265 sc->has_6ch = AC97_IS_6CH(codec->codec_if);
1266 sc->is_fixed = AC97_IS_FIXED_RATE(codec->codec_if);
1267 sc->has_spdif = AC97_HAS_SPDIF(codec->codec_if);
1268
1269 for (i = 0; i < AUIXP_NFORMATS; i++) {
1270 if (sc->is_fixed) {
1271 sc->sc_formats[i].frequency_type = 1;
1272 sc->sc_formats[i].frequency[0] = 48000;
1273 }
1274 switch (sc->sc_formats[i].channels) {
1275 case 4 :
1276 if (sc->has_4ch)
1277 break;
1278 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1279 break;
1280 case 6 :
1281 if (sc->has_6ch)
1282 break;
1283 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1284 break;
1285 default :
1286 break;
1287 }
1288 }
1289
1290 /*
1291 * Create all encodings (and/or -translations) based on the formats
1292 * supported. */
1293 res = auconv_create_encodings(sc->sc_formats, AUIXP_NFORMATS,
1294 &sc->sc_encodings);
1295 if (res) {
1296 printf("%s: auconv_create_encodings failed; "
1297 "no attachments\n", sc->sc_dev.dv_xname);
1298 return;
1299 }
1300
1301 /* attach audio devices for all detected codecs */
1302 /* XXX wise? look at other multiple-codec able chipsets XXX */
1303 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1304 codec = &sc->sc_codec[codec_nr];
1305 if (codec->present)
1306 audio_attach_mi(&auixp_hw_if, codec, &sc->sc_dev);
1307 }
1308
1309 /* done! now enable all interrupts we can service */
1310 auixp_enable_interrupts(sc);
1311 }
1312
1313
1314 static void
1315 auixp_enable_interrupts(struct auixp_softc *sc)
1316 {
1317 bus_space_tag_t iot;
1318 bus_space_handle_t ioh;
1319 uint32_t value;
1320
1321 iot = sc->sc_iot;
1322 ioh = sc->sc_ioh;
1323 /* clear all pending */
1324 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1325
1326 /* enable all relevant interrupt sources we can handle */
1327 value = bus_space_read_4(iot, ioh, ATI_REG_IER);
1328
1329 value |= ATI_REG_IER_IO_STATUS_EN;
1330 #ifdef notyet
1331 value |= ATI_REG_IER_IN_XRUN_EN;
1332 value |= ATI_REG_IER_OUT_XRUN_EN;
1333
1334 value |= ATI_REG_IER_SPDIF_XRUN_EN;
1335 value |= ATI_REG_IER_SPDF_STATUS_EN;
1336 #endif
1337
1338 bus_space_write_4(iot, ioh, ATI_REG_IER, value);
1339 }
1340
1341
1342 static void
1343 auixp_disable_interrupts(struct auixp_softc *sc)
1344 {
1345 bus_space_tag_t iot;
1346 bus_space_handle_t ioh;
1347
1348 iot = sc->sc_iot;
1349 ioh = sc->sc_ioh;
1350 /* disable all interrupt sources */
1351 bus_space_write_4(iot, ioh, ATI_REG_IER, 0);
1352
1353 /* clear all pending */
1354 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1355 }
1356
1357
1358 /* dismantle what we've set up by undoing setup */
1359 static int
1360 auixp_detach(struct device *self, int flags)
1361 {
1362 struct auixp_softc *sc;
1363
1364 sc = (struct auixp_softc *)self;
1365 /* XXX shouldn't we just reset the chip? XXX */
1366 /*
1367 * should we explicitly disable interrupt generation and acknowledge
1368 * what's left on? better be safe than sorry.
1369 */
1370 auixp_disable_interrupts(sc);
1371
1372 /* tear down .... */
1373 config_detach(&sc->sc_dev, flags); /* XXX OK? XXX */
1374
1375 if (sc->sc_ih != NULL)
1376 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1377 if (sc->sc_ios)
1378 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1379
1380 if (sc->savemem)
1381 free(sc->savemem, M_DEVBUF);
1382
1383 return 0;
1384 }
1385
1386
1387 /*
1388 * codec handling
1389 *
1390 * IXP audio support can have upto 3 codecs! are they chained ? or
1391 * alternative outlets with the same audio feed i.e. with different mixer
1392 * settings? XXX does NetBSD support more than one audio codec? XXX
1393 */
1394
1395
1396 static int
1397 auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1398 {
1399 struct auixp_codec *ixp_codec;
1400
1401 ixp_codec = aux;
1402 ixp_codec->codec_if = codec_if;
1403 ixp_codec->present = 1;
1404
1405 return 0;
1406 }
1407
1408
1409 static int
1410 auixp_read_codec(void *aux, uint8_t reg, uint16_t *result)
1411 {
1412 struct auixp_codec *co;
1413 struct auixp_softc *sc;
1414 bus_space_tag_t iot;
1415 bus_space_handle_t ioh;
1416 uint32_t data;
1417 int timeout;
1418
1419 co = aux;
1420 sc = co->sc;
1421 iot = sc->sc_iot;
1422 ioh = sc->sc_ioh;
1423 if (auixp_wait_for_codecs(sc, "read_codec"))
1424 return 0xffff;
1425
1426 /* build up command for reading codec register */
1427 data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1428 ATI_REG_PHYS_OUT_ADDR_EN |
1429 ATI_REG_PHYS_OUT_RW |
1430 co->codec_nr;
1431
1432 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data);
1433
1434 if (auixp_wait_for_codecs(sc, "read_codec"))
1435 return 0xffff;
1436
1437 /* wait until codec info is clocked in */
1438 timeout = 500; /* 500*2 usec -> 0.001 sec */
1439 do {
1440 data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR);
1441 if (data & ATI_REG_PHYS_IN_READ_FLAG) {
1442 DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n",
1443 reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT)
1444 );
1445 *result = data >> ATI_REG_PHYS_IN_DATA_SHIFT;
1446 return 0;
1447 }
1448 DELAY(2);
1449 timeout--;
1450 } while (timeout > 0);
1451
1452 if (reg < 0x7c)
1453 printf("%s: codec read timeout! (reg %x)\n",
1454 sc->sc_dev.dv_xname, reg);
1455
1456 return 0xffff;
1457 }
1458
1459
1460 static int
1461 auixp_write_codec(void *aux, uint8_t reg, uint16_t data)
1462 {
1463 struct auixp_codec *co;
1464 struct auixp_softc *sc;
1465 bus_space_tag_t iot;
1466 bus_space_handle_t ioh;
1467 uint32_t value;
1468
1469 DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data));
1470 co = aux;
1471 sc = co->sc;
1472 iot = sc->sc_iot;
1473 ioh = sc->sc_ioh;
1474 if (auixp_wait_for_codecs(sc, "write_codec"))
1475 return -1;
1476
1477 /* build up command for writing codec register */
1478 value = (((uint32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) |
1479 (((uint32_t) reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1480 ATI_REG_PHYS_OUT_ADDR_EN |
1481 co->codec_nr;
1482
1483 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value);
1484
1485 return 0;
1486 }
1487
1488
1489 static int
1490 auixp_reset_codec(void *aux)
1491 {
1492
1493 /* nothing to be done? */
1494 return 0;
1495 }
1496
1497
1498 static enum ac97_host_flags
1499 auixp_flags_codec(void *aux)
1500 {
1501 struct auixp_codec *ixp_codec;
1502
1503 ixp_codec = aux;
1504 return ixp_codec->codec_flags;
1505 }
1506
1507
1508 static int
1509 auixp_wait_for_codecs(struct auixp_softc *sc, const char *func)
1510 {
1511 bus_space_tag_t iot;
1512 bus_space_handle_t ioh;
1513 uint32_t value;
1514 int timeout;
1515
1516 iot = sc->sc_iot;
1517 ioh = sc->sc_ioh;
1518 /* wait until all codec transfers are done */
1519 timeout = 500; /* 500*2 usec -> 0.001 sec */
1520 do {
1521 value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR);
1522 if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0)
1523 return 0;
1524
1525 DELAY(2);
1526 timeout--;
1527 } while (timeout > 0);
1528
1529 printf("%s: %s: timed out\n", func, sc->sc_dev.dv_xname);
1530 return -1;
1531 }
1532
1533
1534
1535 static void
1536 auixp_autodetect_codecs(struct auixp_softc *sc)
1537 {
1538 bus_space_tag_t iot;
1539 bus_space_handle_t ioh;
1540 struct auixp_codec *codec;
1541 int timeout, codec_nr;
1542
1543 iot = sc->sc_iot;
1544 ioh = sc->sc_ioh;
1545 /* ATI IXP can have upto 3 codecs; mark all codecs as not existing */
1546 sc->sc_codec_not_ready_bits = 0;
1547 sc->sc_num_codecs = 0;
1548
1549 /* enable all codecs to interrupt as well as the new frame interrupt */
1550 bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS);
1551
1552 /* wait for the interrupts to happen */
1553 timeout = 100; /* 100.000 usec -> 0.1 sec */
1554
1555 while (timeout > 0) {
1556 DELAY(1000);
1557 if (sc->sc_codec_not_ready_bits)
1558 break;
1559 timeout--;
1560 }
1561
1562 if (timeout == 0)
1563 printf("%s: WARNING: timeout during codec detection; "
1564 "codecs might be present but haven't interrupted\n",
1565 sc->sc_dev.dv_xname);
1566
1567 /* disable all interrupts for now */
1568 auixp_disable_interrupts(sc);
1569
1570 /* Attach AC97 host interfaces */
1571 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1572 codec = &sc->sc_codec[codec_nr];
1573 bzero(codec, sizeof(struct auixp_codec));
1574
1575 codec->sc = sc;
1576 codec->codec_nr = codec_nr;
1577 codec->present = 0;
1578
1579 codec->host_if.arg = codec;
1580 codec->host_if.attach = auixp_attach_codec;
1581 codec->host_if.read = auixp_read_codec;
1582 codec->host_if.write = auixp_write_codec;
1583 codec->host_if.reset = auixp_reset_codec;
1584 codec->host_if.flags = auixp_flags_codec;
1585 }
1586
1587 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) {
1588 /* codec 0 present */
1589 DPRINTF(("auixp : YAY! codec 0 present!\n"));
1590 if (ac97_attach(&sc->sc_codec[0].host_if, &sc->sc_dev) == 0)
1591 sc->sc_num_codecs++;
1592 }
1593
1594 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) {
1595 /* codec 1 present */
1596 DPRINTF(("auixp : YAY! codec 1 present!\n"));
1597 if (ac97_attach(&sc->sc_codec[1].host_if, &sc->sc_dev) == 0)
1598 sc->sc_num_codecs++;
1599 }
1600
1601 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) {
1602 /* codec 2 present */
1603 DPRINTF(("auixp : YAY! codec 2 present!\n"));
1604 if (ac97_attach(&sc->sc_codec[2].host_if, &sc->sc_dev) == 0)
1605 sc->sc_num_codecs++;
1606 }
1607
1608 if (sc->sc_num_codecs == 0) {
1609 printf("%s: no codecs detected or "
1610 "no codecs managed to initialise\n",
1611 sc->sc_dev.dv_xname);
1612 return;
1613 }
1614
1615 }
1616
1617
1618
1619 /* initialisation routines */
1620
1621 static void
1622 auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1623 {
1624 bus_space_tag_t iot;
1625 bus_space_handle_t ioh;
1626 uint32_t value;
1627
1628 iot = sc->sc_iot;
1629 ioh = sc->sc_ioh;
1630 /* lets not stress the DMA engine more than nessisary */
1631 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1632 if (value & dma->dma_enable_bit) {
1633 value &= ~dma->dma_enable_bit;
1634 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1635 }
1636 }
1637
1638
1639 static void
1640 auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1641 {
1642 bus_space_tag_t iot;
1643 bus_space_handle_t ioh;
1644 uint32_t value;
1645
1646 iot = sc->sc_iot;
1647 ioh = sc->sc_ioh;
1648 /* lets not stress the DMA engine more than nessisary */
1649 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1650 if (!(value & dma->dma_enable_bit)) {
1651 value |= dma->dma_enable_bit;
1652 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1653 }
1654 }
1655
1656
1657 static void
1658 auixp_reset_aclink(struct auixp_softc *sc)
1659 {
1660 bus_space_tag_t iot;
1661 bus_space_handle_t ioh;
1662 uint32_t value, timeout;
1663
1664 iot = sc->sc_iot;
1665 ioh = sc->sc_ioh;
1666
1667 /* if power is down, power it up */
1668 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1669 if (value & ATI_REG_CMD_POWERDOWN) {
1670 printf("%s: powering up\n", sc->sc_dev.dv_xname);
1671
1672 /* explicitly enable power */
1673 value &= ~ATI_REG_CMD_POWERDOWN;
1674 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1675
1676 /* have to wait at least 10 usec for it to initialise */
1677 DELAY(20);
1678 };
1679
1680 printf("%s: soft resetting aclink\n", sc->sc_dev.dv_xname);
1681
1682 /* perform a soft reset */
1683 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1684 value |= ATI_REG_CMD_AC_SOFT_RESET;
1685 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1686
1687 /* need to read the CMD reg and wait aprox. 10 usec to init */
1688 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1689 DELAY(20);
1690
1691 /* clear soft reset flag again */
1692 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1693 value &= ~ATI_REG_CMD_AC_SOFT_RESET;
1694 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1695
1696 /* check if the ac-link is working; reset device otherwise */
1697 timeout = 10;
1698 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1699 while (!(value & ATI_REG_CMD_ACLINK_ACTIVE)) {
1700 printf("%s: not up; resetting aclink hardware\n",
1701 sc->sc_dev.dv_xname);
1702
1703 /* dip aclink reset but keep the acsync */
1704 value &= ~ATI_REG_CMD_AC_RESET;
1705 value |= ATI_REG_CMD_AC_SYNC;
1706 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1707
1708 /* need to read CMD again and wait again (clocking in issue?) */
1709 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1710 DELAY(20);
1711
1712 /* assert aclink reset again */
1713 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1714 value |= ATI_REG_CMD_AC_RESET;
1715 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1716
1717 /* check if its active now */
1718 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1719
1720 timeout--;
1721 if (timeout == 0) break;
1722 };
1723
1724 if (timeout == 0) {
1725 printf("%s: giving up aclink reset\n", sc->sc_dev.dv_xname);
1726 };
1727 if (timeout != 10) {
1728 printf("%s: aclink hardware reset successful\n",
1729 sc->sc_dev.dv_xname);
1730 };
1731
1732 /* assert reset and sync for safety */
1733 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1734 value |= ATI_REG_CMD_AC_SYNC | ATI_REG_CMD_AC_RESET;
1735 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1736 }
1737
1738
1739 /* chip hard init */
1740 static int
1741 auixp_init(struct auixp_softc *sc)
1742 {
1743 bus_space_tag_t iot;
1744 bus_space_handle_t ioh;
1745 uint32_t value;
1746
1747 iot = sc->sc_iot;
1748 ioh = sc->sc_ioh;
1749 /* disable all interrupts and clear all sources */
1750 auixp_disable_interrupts(sc);
1751
1752 /* clear all DMA enables (preserving rest of settings) */
1753 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1754 value &= ~( ATI_REG_CMD_IN_DMA_EN |
1755 ATI_REG_CMD_OUT_DMA_EN |
1756 ATI_REG_CMD_SPDF_OUT_EN );
1757 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1758
1759 /* Reset AC-link */
1760 auixp_reset_aclink(sc);
1761
1762 /*
1763 * codecs get auto-detected later
1764 *
1765 * note: we are NOT enabling interrupts yet, no codecs have been
1766 * detected yet nor is anything else set up
1767 */
1768
1769 return 0;
1770 }
1771
1772
1773 /*
1774 * TODO power saving and suspend / resume support
1775 *
1776 */
1777
1778 static int
1779 auixp_power(struct auixp_softc *sc, int state)
1780 {
1781 pcitag_t tag;
1782 pci_chipset_tag_t pc;
1783 pcireg_t data;
1784 int pmcapreg;
1785
1786 tag = sc->sc_tag;
1787 pc = sc->sc_pct;
1788 if (pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &pmcapreg, 0)) {
1789 data = pci_conf_read(pc, tag, pmcapreg + PCI_PMCSR);
1790 if ((data & PCI_PMCSR_STATE_MASK) != state)
1791 pci_conf_write(pc, tag, pmcapreg + PCI_PMCSR, state);
1792 }
1793
1794 return 0;
1795 }
1796
1797
1798 #if 0
1799 static void
1800 auixp_powerhook(int why, void *hdl)
1801 {
1802 struct auixp_softc *sc;
1803
1804 sc = (struct auixp_softc *)hdl;
1805 switch (why) {
1806 case PWR_SUSPEND:
1807 case PWR_STANDBY:
1808 auixp_suspend(sc);
1809 break;
1810 case PWR_RESUME:
1811 auixp_resume(sc);
1812 /* XXX fix me XXX */
1813 // (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1814 break;
1815 }
1816 }
1817
1818
1819 static int
1820 auixp_suspend(struct auixp_softc *sc)
1821 {
1822
1823 /* XXX no power functions yet XXX */
1824 return 0;
1825 }
1826
1827
1828 static int
1829 auixp_resume(struct auixp_softc *sc)
1830 {
1831
1832 /* XXX no power functions yet XXX */
1833 return 0;
1834 }
1835 #endif /* 0 */
1836
1837 #ifdef DEBUG_AUIXP
1838
1839 static void
1840 auixp_dumpreg(void)
1841 {
1842 struct auixp_softc *sc;
1843 bus_space_tag_t iot;
1844 bus_space_handle_t ioh;
1845 int i;
1846
1847 sc = static_sc;
1848 iot = sc->sc_iot;
1849 ioh = sc->sc_ioh;
1850 printf("%s register dump:\n", sc->sc_dev.dv_xname);
1851 for (i = 0; i < 256; i+=4) {
1852 printf("\t0x%02x: 0x%08x\n", i, bus_space_read_4(iot, ioh, i));
1853 }
1854 printf("\n");
1855 }
1856 #endif
1857