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