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