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