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