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