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