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