auixp.c revision 1.45 1 /* $NetBSD: auixp.c,v 1.45 2019/03/16 12:09:58 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.45 2019/03/16 12:09:58 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_if.h>
71 #include <dev/mulaw.h>
72 #include <dev/auconv.h>
73
74 #include <dev/ic/ac97var.h>
75 #include <dev/ic/ac97reg.h>
76
77 #include <dev/pci/pcidevs.h>
78 #include <dev/pci/pcivar.h>
79 #include <dev/pci/auixpreg.h>
80 #include <dev/pci/auixpvar.h>
81
82
83 /* #define DEBUG_AUIXP */
84
85
86 /* why isn't this base address register not in the headerfile? */
87 #define PCI_CBIO 0x10
88
89
90 /* macro's used */
91 #define KERNADDR(p) ((void *)((p)->addr))
92 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
93
94
95 /* the differences might be irrelevant */
96 enum {
97 IXP_200,
98 IXP_300,
99 IXP_400
100 };
101
102
103 /* our `cards' */
104 static const struct auixp_card_type {
105 uint16_t pci_vendor_id;
106 uint16_t pci_product_id;
107 int type;
108 } auixp_card_types[] = {
109 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_200, IXP_200 },
110 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_300, IXP_300 },
111 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_400, IXP_400 },
112 { 0, 0, 0 }
113 };
114
115
116 struct audio_device auixp_device = {
117 "ATI IXP audio",
118 "",
119 "auixp"
120 };
121
122
123 /* codec detection constant indicating the interrupt flags */
124 #define ALL_CODECS_NOT_READY \
125 (ATI_REG_ISR_CODEC0_NOT_READY |\
126 ATI_REG_ISR_CODEC1_NOT_READY |\
127 ATI_REG_ISR_CODEC2_NOT_READY)
128 #define CODEC_CHECK_BITS (ALL_CODECS_NOT_READY|ATI_REG_ISR_NEW_FRAME)
129
130
131 /* autoconfig */
132 static int auixp_match(device_t, cfdata_t, void *);
133 static void auixp_attach(device_t, device_t, void *);
134 static int auixp_detach(device_t, int);
135
136
137 /* audio(9) function prototypes */
138 static int auixp_query_encoding(void *, struct audio_encoding *);
139 static int auixp_set_params(void *, int, int, audio_params_t *,
140 audio_params_t *,
141 stream_filter_list_t *, stream_filter_list_t *);
142 static int auixp_commit_settings(void *);
143 static int auixp_round_blocksize(void *, int, int, const audio_params_t *);
144 static int auixp_trigger_output(void *, void *, void *, int,
145 void (*)(void *),
146 void *, const audio_params_t *);
147 static int auixp_trigger_input(void *, void *, void *, int,
148 void (*)(void *),
149 void *, const audio_params_t *);
150 static int auixp_halt_output(void *);
151 static int auixp_halt_input(void *);
152 static int auixp_set_port(void *, mixer_ctrl_t *);
153 static int auixp_get_port(void *, mixer_ctrl_t *);
154 static int auixp_query_devinfo(void *, mixer_devinfo_t *);
155 static void * auixp_malloc(void *, int, size_t);
156 static void auixp_free(void *, void *, size_t);
157 static int auixp_getdev(void *, struct audio_device *);
158 static size_t auixp_round_buffersize(void *, int, size_t);
159 static int auixp_get_props(void *);
160 static int auixp_intr(void *);
161 static int auixp_allocmem(struct auixp_softc *, size_t, size_t,
162 struct auixp_dma *);
163 static int auixp_freemem(struct auixp_softc *, struct auixp_dma *);
164 static paddr_t auixp_mappage(void *, void *, off_t, int);
165
166 /* Supporting subroutines */
167 static int auixp_init(struct auixp_softc *);
168 static void auixp_autodetect_codecs(struct auixp_softc *);
169 static void auixp_post_config(device_t);
170
171 static void auixp_reset_aclink(struct auixp_softc *);
172 static int auixp_attach_codec(void *, struct ac97_codec_if *);
173 static int auixp_read_codec(void *, uint8_t, uint16_t *);
174 static int auixp_write_codec(void *, uint8_t, uint16_t);
175 static int auixp_wait_for_codecs(struct auixp_softc *, const char *);
176 static int auixp_reset_codec(void *);
177 static enum ac97_host_flags auixp_flags_codec(void *);
178
179 static void auixp_enable_dma(struct auixp_softc *, struct auixp_dma *);
180 static void auixp_disable_dma(struct auixp_softc *, struct auixp_dma *);
181 static void auixp_enable_interrupts(struct auixp_softc *);
182 static void auixp_disable_interrupts(struct auixp_softc *);
183
184
185 /* statics */
186 static void auixp_link_daisychain(struct auixp_softc *,
187 struct auixp_dma *, struct auixp_dma *,
188 int, int);
189 static int auixp_allocate_dma_chain(struct auixp_softc *,
190 struct auixp_dma **);
191 static void auixp_program_dma_chain(struct auixp_softc *,
192 struct auixp_dma *);
193 static void auixp_dma_update(struct auixp_softc *, struct auixp_dma *);
194 static void auixp_update_busbusy(struct auixp_softc *);
195 static void auixp_get_locks(void *, kmutex_t **, kmutex_t **);
196
197 static bool auixp_resume(device_t, const pmf_qual_t *);
198
199
200 #ifdef DEBUG_AUIXP
201 static struct auixp_softc *static_sc;
202 static void auixp_dumpreg(void);
203 # define DPRINTF(x) printf x;
204 #else
205 # define DPRINTF(x)
206 #endif
207
208
209 static const struct audio_hw_if auixp_hw_if = {
210 .query_encoding = auixp_query_encoding,
211 .set_params = auixp_set_params,
212 .round_blocksize = auixp_round_blocksize,
213 .commit_settings = auixp_commit_settings,
214 .halt_output = auixp_halt_output,
215 .halt_input = auixp_halt_input,
216 .getdev = auixp_getdev,
217 .set_port = auixp_set_port,
218 .get_port = auixp_get_port,
219 .query_devinfo = auixp_query_devinfo,
220 .allocm = auixp_malloc,
221 .freem = auixp_free,
222 .round_buffersize = auixp_round_buffersize,
223 .mappage = auixp_mappage,
224 .get_props = auixp_get_props,
225 .trigger_output = auixp_trigger_output,
226 .trigger_input = auixp_trigger_input,
227 .get_locks = auixp_get_locks,
228 };
229
230
231 CFATTACH_DECL_NEW(auixp, sizeof(struct auixp_softc), auixp_match, auixp_attach,
232 auixp_detach, NULL);
233
234
235 /*
236 * audio(9) functions
237 */
238
239 static int
240 auixp_query_encoding(void *hdl, struct audio_encoding *ae)
241 {
242 struct auixp_codec *co;
243 struct auixp_softc *sc;
244
245 co = (struct auixp_codec *) hdl;
246 sc = co->sc;
247 return auconv_query_encoding(sc->sc_encodings, ae);
248 }
249
250
251 static int
252 auixp_set_rate(struct auixp_codec *co, int mode, u_int srate)
253 {
254 int ret;
255 u_int ratetmp;
256
257 ratetmp = srate;
258 if (mode == AUMODE_RECORD) {
259 ret = co->codec_if->vtbl->set_rate(co->codec_if,
260 AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
261 return ret;
262 }
263
264 /* play mode */
265 ret = co->codec_if->vtbl->set_rate(co->codec_if,
266 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
267 if (ret)
268 return ret;
269
270 ratetmp = srate;
271 ret = co->codec_if->vtbl->set_rate(co->codec_if,
272 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
273 if (ret)
274 return ret;
275
276 ratetmp = srate;
277 ret = co->codec_if->vtbl->set_rate(co->codec_if,
278 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
279 return ret;
280 }
281
282
283 /* commit setting and program ATI IXP chip */
284 static int
285 auixp_commit_settings(void *hdl)
286 {
287 struct auixp_codec *co;
288 struct auixp_softc *sc;
289 bus_space_tag_t iot;
290 bus_space_handle_t ioh;
291 struct audio_params *params;
292 uint32_t value;
293
294 /* XXX would it be better to stop interrupts first? XXX */
295 co = (struct auixp_codec *) hdl;
296 sc = co->sc;
297 iot = sc->sc_iot;
298 ioh = sc->sc_ioh;
299
300 /* process input settings */
301 params = &sc->sc_play_params;
302
303 /* set input interleaving (precision) */
304 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
305 value &= ~ATI_REG_CMD_INTERLEAVE_IN;
306 if (params->precision <= 16)
307 value |= ATI_REG_CMD_INTERLEAVE_IN;
308 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
309
310 /* process output settings */
311 params = &sc->sc_play_params;
312
313 value = bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT);
314 value &= ~ATI_REG_OUT_DMA_SLOT_MASK;
315
316 /* TODO SPDIF case for 8 channels */
317 switch (params->channels) {
318 case 6:
319 value |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
320 ATI_REG_OUT_DMA_SLOT_BIT(8);
321 /* fallthru */
322 case 4:
323 value |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
324 ATI_REG_OUT_DMA_SLOT_BIT(9);
325 /* fallthru */
326 default:
327 value |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
328 ATI_REG_OUT_DMA_SLOT_BIT(4);
329 break;
330 }
331 /* set output threshold */
332 value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
333 bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value);
334
335 /* set output interleaving (precision) */
336 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
337 value &= ~ATI_REG_CMD_INTERLEAVE_OUT;
338 if (params->precision <= 16)
339 value |= ATI_REG_CMD_INTERLEAVE_OUT;
340 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
341
342 /* enable 6 channel reordering */
343 value = bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER);
344 value &= ~ATI_REG_6CH_REORDER_EN;
345 if (params->channels == 6)
346 value |= ATI_REG_6CH_REORDER_EN;
347 bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value);
348
349 if (sc->has_spdif) {
350 /* set SPDIF (if present) */
351 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
352 value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK;
353 value |= ATI_REG_CMD_SPDF_CONFIG_34; /* NetBSD AC'97 default */
354
355 /* XXX this prolly is not nessisary unless splitted XXX */
356 value &= ~ATI_REG_CMD_INTERLEAVE_SPDF;
357 if (params->precision <= 16)
358 value |= ATI_REG_CMD_INTERLEAVE_SPDF;
359 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
360 }
361
362 return 0;
363 }
364
365
366 /* set audio properties in desired setting */
367 static int
368 auixp_set_params(void *hdl, int setmode, int usemode,
369 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
370 stream_filter_list_t *rfil)
371 {
372 struct auixp_codec *co;
373 struct auixp_softc *sc;
374 audio_params_t *params;
375 stream_filter_list_t *fil;
376 int mode, index;
377
378 /*
379 * In current NetBSD AC'97 implementation, SPDF is linked to channel 3
380 * and 4 i.e. stereo output.
381 */
382
383 co = (struct auixp_codec *) hdl;
384 sc = co->sc;
385 for (mode = AUMODE_RECORD; mode != -1;
386 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
387 if ((setmode & mode) == 0)
388 continue;
389
390 params = (mode == AUMODE_PLAY) ? play : rec;
391 fil = (mode == AUMODE_PLAY) ? pfil : rfil;
392 if (params == NULL)
393 continue;
394
395 /* AD1888 settings ... don't know the IXP limits */
396 if (params->sample_rate < AUIXP_MINRATE)
397 return EINVAL;
398 if (params->sample_rate > AUIXP_MAXRATE)
399 return EINVAL;
400
401 index = auconv_set_converter(sc->sc_formats, AUIXP_NFORMATS,
402 mode, params, TRUE, fil);
403
404 /* nothing found? */
405 if (index < 0)
406 return EINVAL;
407
408 /* not sure yet as to why i have to change params here */
409 if (fil->req_size > 0)
410 params = &fil->filters[0].param;
411
412 /* if variable speed and we can't set the desired rate, fail */
413 if ((sc->sc_formats[index].frequency_type != 1) &&
414 auixp_set_rate(co, mode, params->sample_rate))
415 return EINVAL;
416
417 /* preserve the settings */
418 if (mode == AUMODE_PLAY)
419 sc->sc_play_params = *params;
420 if (mode == AUMODE_RECORD)
421 sc->sc_rec_params = *params;
422 }
423
424 return 0;
425 }
426
427
428 /* called to translate a requested blocksize to a hw-possible one */
429 static int
430 auixp_round_blocksize(void *hdl, int bs, int mode,
431 const audio_params_t *param)
432 {
433 uint32_t new_bs;
434
435 new_bs = bs;
436 /* Be conservative; align to 32 bytes and maximise it to 64 kb */
437 /* 256 kb possible */
438 if (new_bs > 0x10000)
439 bs = 0x10000; /* 64 kb max */
440 new_bs = (bs & ~0x20); /* 32 bytes align */
441
442 return new_bs;
443 }
444
445
446 /*
447 * allocate dma capable memory and record its information for later retrieval
448 * when we program the dma chain itself. The trigger routines passes on the
449 * kernel virtual address we return here as a reference to the mapping.
450 */
451 static void *
452 auixp_malloc(void *hdl, int direction, size_t size)
453 {
454 struct auixp_codec *co;
455 struct auixp_softc *sc;
456 struct auixp_dma *dma;
457 int error;
458
459 co = (struct auixp_codec *) hdl;
460 sc = co->sc;
461 /* get us a auixp_dma structure */
462 dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
463
464 /* get us a dma buffer itself */
465 error = auixp_allocmem(sc, size, 16, dma);
466 if (error) {
467 kmem_free(dma, sizeof(*dma));
468 aprint_error_dev(sc->sc_dev, "auixp_malloc: not enough memory\n");
469
470 return NULL;
471 }
472 SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain);
473
474 DPRINTF(("auixp_malloc: returning kern %p, hw 0x%08x for %d bytes "
475 "in %d segs\n", KERNADDR(dma), (uint32_t) DMAADDR(dma), dma->size,
476 dma->nsegs)
477 );
478
479 return KERNADDR(dma);
480 }
481
482
483 /*
484 * free and release dma capable memory we allocated before and remove its
485 * recording
486 */
487 static void
488 auixp_free(void *hdl, void *addr, size_t size)
489 {
490 struct auixp_codec *co;
491 struct auixp_softc *sc;
492 struct auixp_dma *dma;
493
494 co = (struct auixp_codec *) hdl;
495 sc = co->sc;
496 SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) {
497 if (KERNADDR(dma) == addr) {
498 SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma,
499 dma_chain);
500 auixp_freemem(sc, dma);
501 kmem_free(dma, sizeof(*dma));
502 return;
503 }
504 }
505 }
506
507
508 static int
509 auixp_getdev(void *hdl, struct audio_device *ret)
510 {
511
512 *ret = auixp_device;
513 return 0;
514 }
515
516
517 /* pass request to AC'97 codec code */
518 static int
519 auixp_set_port(void *hdl, mixer_ctrl_t *mc)
520 {
521 struct auixp_codec *co;
522
523 co = (struct auixp_codec *) hdl;
524 return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc);
525 }
526
527
528 /* pass request to AC'97 codec code */
529 static int
530 auixp_get_port(void *hdl, mixer_ctrl_t *mc)
531 {
532 struct auixp_codec *co;
533
534 co = (struct auixp_codec *) hdl;
535 return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc);
536 }
537
538 /* pass request to AC'97 codec code */
539 static int
540 auixp_query_devinfo(void *hdl, mixer_devinfo_t *di)
541 {
542 struct auixp_codec *co;
543
544 co = (struct auixp_codec *) hdl;
545 return co->codec_if->vtbl->query_devinfo(co->codec_if, di);
546 }
547
548
549 static size_t
550 auixp_round_buffersize(void *hdl, int direction,
551 size_t bufsize)
552 {
553
554 /* XXX force maximum? i.e. 256 kb? */
555 return bufsize;
556 }
557
558
559 static int
560 auixp_get_props(void *hdl)
561 {
562
563 return AUDIO_PROP_MMAP | 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 /* memory map dma memory */
1023 static paddr_t
1024 auixp_mappage(void *hdl, void *mem, off_t off, int prot)
1025 {
1026 struct auixp_codec *co;
1027 struct auixp_softc *sc;
1028 struct auixp_dma *p;
1029
1030 co = (struct auixp_codec *) hdl;
1031 sc = co->sc;
1032 /* for sanity */
1033 if (off < 0)
1034 return -1;
1035
1036 /* look up allocated DMA area */
1037 SLIST_FOREACH(p, &sc->sc_dma_list, dma_chain) {
1038 if (KERNADDR(p) == mem)
1039 break;
1040 }
1041
1042 /* have we found it ? */
1043 if (!p)
1044 return -1;
1045
1046 /* return mmap'd region */
1047 return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
1048 off, prot, BUS_DMA_WAITOK);
1049 }
1050
1051
1052 /*
1053 * Attachment section
1054 */
1055
1056 /* Is it my hardware? */
1057 static int
1058 auixp_match(device_t dev, cfdata_t match, void *aux)
1059 {
1060 struct pci_attach_args *pa;
1061
1062 pa = (struct pci_attach_args *)aux;
1063 switch(PCI_VENDOR(pa->pa_id)) {
1064 case PCI_VENDOR_ATI:
1065 switch(PCI_PRODUCT(pa->pa_id)) {
1066 case PCI_PRODUCT_ATI_IXP_AUDIO_200:
1067 case PCI_PRODUCT_ATI_IXP_AUDIO_300:
1068 case PCI_PRODUCT_ATI_IXP_AUDIO_400:
1069 return 1;
1070 }
1071 }
1072
1073 return 0;
1074 }
1075
1076
1077 /* it is... now hook up and set up the resources we need */
1078 static void
1079 auixp_attach(device_t parent, device_t self, void *aux)
1080 {
1081 struct auixp_softc *sc;
1082 struct pci_attach_args *pa;
1083 pcitag_t tag;
1084 pci_chipset_tag_t pc;
1085 pci_intr_handle_t ih;
1086 const struct auixp_card_type *card;
1087 const char *intrstr;
1088 uint32_t data;
1089 int error;
1090 char intrbuf[PCI_INTRSTR_LEN];
1091
1092 sc = device_private(self);
1093 sc->sc_dev = self;
1094 pa = (struct pci_attach_args *)aux;
1095 tag = pa->pa_tag;
1096 pc = pa->pa_pc;
1097 #ifdef DEBUG_AUIXP
1098 static_sc = sc;
1099 #endif
1100
1101 /* print information confirming attachment */
1102 pci_aprint_devinfo(pa, "Audio controller");
1103
1104 /* set up details from our set of known `cards'/chips */
1105 for (card = auixp_card_types; card->pci_vendor_id; card++)
1106 if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
1107 PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
1108 sc->type = card->type;
1109 break;
1110 }
1111
1112 /* device only has 32 bit non prefetchable memory */
1113 /* set MEM space access and enable the card's busmastering */
1114 data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
1115 data |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
1116 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
1117
1118 /* map memory; its not sized -> what is the size? max PCI slot size? */
1119 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_MEM, 0,
1120 &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
1121 aprint_error_dev(sc->sc_dev, "can't map memory space\n");
1122 return;
1123 }
1124
1125 /* Initialize softc */
1126 sc->sc_tag = tag;
1127 sc->sc_pct = pc;
1128 sc->sc_dmat = pa->pa_dmat;
1129 SLIST_INIT(&sc->sc_dma_list);
1130
1131 /* get us the auixp_dma structures */
1132 auixp_allocate_dma_chain(sc, &sc->sc_output_dma);
1133 auixp_allocate_dma_chain(sc, &sc->sc_input_dma);
1134
1135 /* when that fails we are dead in the water */
1136 if (!sc->sc_output_dma || !sc->sc_input_dma)
1137 return;
1138
1139 #if 0
1140 /* could preliminary program DMA chain */
1141 auixp_program_dma_chain(sc, sc->sc_output_dma);
1142 auixp_program_dma_chain(sc, sc->sc_input_dma);
1143 #endif
1144
1145 /* map interrupt on the pci bus */
1146 if (pci_intr_map(pa, &ih)) {
1147 aprint_error_dev(sc->sc_dev, "can't map interrupt\n");
1148 return;
1149 }
1150
1151 /* where are we connected at ? */
1152 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
1153
1154 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
1155 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
1156
1157 /* establish interrupt routine hookup at IPL_AUDIO level */
1158 sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, auixp_intr,
1159 self, device_xname(self));
1160 if (sc->sc_ih == NULL) {
1161 aprint_error_dev(sc->sc_dev, "can't establish interrupt");
1162 if (intrstr != NULL)
1163 aprint_error(" at %s", intrstr);
1164 aprint_error("\n");
1165 return;
1166 }
1167 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
1168
1169 /* power up chip */
1170 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
1171 pci_activate_null)) && error != EOPNOTSUPP) {
1172 aprint_error_dev(sc->sc_dev, "cannot activate %d\n",
1173 error);
1174 return;
1175 }
1176
1177 /* init chip */
1178 if (auixp_init(sc) == -1) {
1179 aprint_error_dev(sc->sc_dev,
1180 "auixp_attach: unable to initialize the card\n");
1181 return;
1182 }
1183
1184 if (!pmf_device_register(self, NULL, auixp_resume))
1185 aprint_error_dev(self, "couldn't establish power handler\n");
1186
1187 /*
1188 * delay further configuration of codecs and audio after interrupts
1189 * are enabled.
1190 */
1191 config_interrupts(self, auixp_post_config);
1192 }
1193
1194
1195 /* called from autoconfigure system when interrupts are enabled */
1196 static void
1197 auixp_post_config(device_t self)
1198 {
1199 struct auixp_softc *sc;
1200 struct auixp_codec *codec;
1201 int codec_nr;
1202 int res, i;
1203
1204 sc = device_private(self);
1205 /* detect the AC97 codecs */
1206 auixp_autodetect_codecs(sc);
1207
1208 /* setup audio translation formats : following codec0 (!) */
1209 codec = &sc->sc_codec[0];
1210 if (!codec->present) {
1211 /* nothing??? then invalidate all formats */
1212 for (i = 0; i < AUIXP_NFORMATS; i++) {
1213 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1214 }
1215 return;
1216 }
1217
1218 /* copy formats and invalidate entries not suitable for codec0 */
1219 memcpy(sc->sc_formats, auixp_formats, sizeof(auixp_formats));
1220 mutex_enter(&sc->sc_lock);
1221 sc->has_4ch = AC97_IS_4CH(codec->codec_if);
1222 sc->has_6ch = AC97_IS_6CH(codec->codec_if);
1223 sc->is_fixed = AC97_IS_FIXED_RATE(codec->codec_if);
1224 sc->has_spdif = AC97_HAS_SPDIF(codec->codec_if);
1225 mutex_exit(&sc->sc_lock);
1226
1227 for (i = 0; i < AUIXP_NFORMATS; i++) {
1228 if (sc->is_fixed) {
1229 sc->sc_formats[i].frequency_type = 1;
1230 sc->sc_formats[i].frequency[0] = 48000;
1231 }
1232 switch (sc->sc_formats[i].channels) {
1233 case 4 :
1234 if (sc->has_4ch)
1235 break;
1236 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1237 break;
1238 case 6 :
1239 if (sc->has_6ch)
1240 break;
1241 AUFMT_INVALIDATE(&sc->sc_formats[i]);
1242 break;
1243 default :
1244 break;
1245 }
1246 }
1247
1248 /*
1249 * Create all encodings (and/or -translations) based on the formats
1250 * supported. */
1251 res = auconv_create_encodings(sc->sc_formats, AUIXP_NFORMATS,
1252 &sc->sc_encodings);
1253 if (res) {
1254 printf("%s: auconv_create_encodings failed; "
1255 "no attachments\n", device_xname(sc->sc_dev));
1256 return;
1257 }
1258
1259 if (sc->has_spdif) {
1260 aprint_normal_dev(sc->sc_dev, "codec spdif support detected but disabled "
1261 "for now\n");
1262 sc->has_spdif = 0;
1263 }
1264
1265 /* fill in the missing details about the dma channels. */
1266 /* for output */
1267 sc->sc_output_dma->linkptr = ATI_REG_OUT_DMA_LINKPTR;
1268 sc->sc_output_dma->dma_enable_bit = ATI_REG_CMD_OUT_DMA_EN |
1269 ATI_REG_CMD_SEND_EN;
1270 /* have spdif? then this too! XXX not seeing LED yet! XXX */
1271 if (sc->has_spdif)
1272 sc->sc_output_dma->dma_enable_bit |= ATI_REG_CMD_SPDF_OUT_EN;
1273
1274 /* and for input */
1275 sc->sc_input_dma->linkptr = ATI_REG_IN_DMA_LINKPTR;
1276 sc->sc_input_dma->dma_enable_bit = ATI_REG_CMD_IN_DMA_EN |
1277 ATI_REG_CMD_RECEIVE_EN;
1278
1279 /* attach audio devices for all detected codecs */
1280 /* XXX wise? look at other multiple-codec able chipsets XXX */
1281 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1282 codec = &sc->sc_codec[codec_nr];
1283 if (codec->present)
1284 audio_attach_mi(&auixp_hw_if, codec, sc->sc_dev);
1285 }
1286
1287 /* done! now enable all interrupts we can service */
1288 auixp_enable_interrupts(sc);
1289 }
1290
1291 static void
1292 auixp_enable_interrupts(struct auixp_softc *sc)
1293 {
1294 bus_space_tag_t iot;
1295 bus_space_handle_t ioh;
1296 uint32_t value;
1297
1298 iot = sc->sc_iot;
1299 ioh = sc->sc_ioh;
1300
1301 mutex_spin_enter(&sc->sc_intr_lock);
1302
1303 /* clear all pending */
1304 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1305
1306 /* enable all relevant interrupt sources we can handle */
1307 value = bus_space_read_4(iot, ioh, ATI_REG_IER);
1308
1309 value |= ATI_REG_IER_IO_STATUS_EN;
1310 #ifdef notyet
1311 value |= ATI_REG_IER_IN_XRUN_EN;
1312 value |= ATI_REG_IER_OUT_XRUN_EN;
1313
1314 value |= ATI_REG_IER_SPDIF_XRUN_EN;
1315 value |= ATI_REG_IER_SPDF_STATUS_EN;
1316 #endif
1317
1318 bus_space_write_4(iot, ioh, ATI_REG_IER, value);
1319
1320 mutex_spin_exit(&sc->sc_intr_lock);
1321 }
1322
1323
1324 static void
1325 auixp_disable_interrupts(struct auixp_softc *sc)
1326 {
1327 bus_space_tag_t iot;
1328 bus_space_handle_t ioh;
1329
1330 iot = sc->sc_iot;
1331 ioh = sc->sc_ioh;
1332
1333 mutex_spin_enter(&sc->sc_intr_lock);
1334
1335 /* disable all interrupt sources */
1336 bus_space_write_4(iot, ioh, ATI_REG_IER, 0);
1337
1338 /* clear all pending */
1339 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
1340
1341 mutex_spin_exit(&sc->sc_intr_lock);
1342 }
1343
1344
1345 /* dismantle what we've set up by undoing setup */
1346 static int
1347 auixp_detach(device_t self, int flags)
1348 {
1349 struct auixp_softc *sc;
1350
1351 sc = device_private(self);
1352 /* XXX shouldn't we just reset the chip? XXX */
1353 /*
1354 * should we explicitly disable interrupt generation and acknowledge
1355 * what's left on? better be safe than sorry.
1356 */
1357 auixp_disable_interrupts(sc);
1358
1359 /* tear down .... */
1360 config_detach(sc->sc_dev, flags); /* XXX OK? XXX */
1361 pmf_device_deregister(self);
1362
1363 if (sc->sc_ih != NULL)
1364 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1365 if (sc->sc_ios)
1366 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1367
1368 mutex_destroy(&sc->sc_lock);
1369 mutex_destroy(&sc->sc_intr_lock);
1370
1371 return 0;
1372 }
1373
1374
1375 /*
1376 * codec handling
1377 *
1378 * IXP audio support can have upto 3 codecs! are they chained ? or
1379 * alternative outlets with the same audio feed i.e. with different mixer
1380 * settings? XXX does NetBSD support more than one audio codec? XXX
1381 */
1382
1383
1384 static int
1385 auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1386 {
1387 struct auixp_codec *ixp_codec;
1388
1389 ixp_codec = aux;
1390 ixp_codec->codec_if = codec_if;
1391 ixp_codec->present = 1;
1392
1393 return 0;
1394 }
1395
1396
1397 static int
1398 auixp_read_codec(void *aux, uint8_t reg, uint16_t *result)
1399 {
1400 struct auixp_codec *co;
1401 struct auixp_softc *sc;
1402 bus_space_tag_t iot;
1403 bus_space_handle_t ioh;
1404 uint32_t data;
1405 int timeout;
1406
1407 co = aux;
1408 sc = co->sc;
1409 iot = sc->sc_iot;
1410 ioh = sc->sc_ioh;
1411 if (auixp_wait_for_codecs(sc, "read_codec"))
1412 return 0xffff;
1413
1414 /* build up command for reading codec register */
1415 data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1416 ATI_REG_PHYS_OUT_ADDR_EN |
1417 ATI_REG_PHYS_OUT_RW |
1418 co->codec_nr;
1419
1420 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data);
1421
1422 if (auixp_wait_for_codecs(sc, "read_codec"))
1423 return 0xffff;
1424
1425 /* wait until codec info is clocked in */
1426 timeout = 500; /* 500*2 usec -> 0.001 sec */
1427 do {
1428 data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR);
1429 if (data & ATI_REG_PHYS_IN_READ_FLAG) {
1430 DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n",
1431 reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT)
1432 );
1433 *result = data >> ATI_REG_PHYS_IN_DATA_SHIFT;
1434 return 0;
1435 }
1436 DELAY(2);
1437 timeout--;
1438 } while (timeout > 0);
1439
1440 if (reg < 0x7c)
1441 printf("%s: codec read timeout! (reg %x)\n",
1442 device_xname(sc->sc_dev), reg);
1443
1444 return 0xffff;
1445 }
1446
1447
1448 static int
1449 auixp_write_codec(void *aux, uint8_t reg, uint16_t data)
1450 {
1451 struct auixp_codec *co;
1452 struct auixp_softc *sc;
1453 bus_space_tag_t iot;
1454 bus_space_handle_t ioh;
1455 uint32_t value;
1456
1457 DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data));
1458 co = aux;
1459 sc = co->sc;
1460 iot = sc->sc_iot;
1461 ioh = sc->sc_ioh;
1462 if (auixp_wait_for_codecs(sc, "write_codec"))
1463 return -1;
1464
1465 /* build up command for writing codec register */
1466 value = (((uint32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) |
1467 (((uint32_t) reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
1468 ATI_REG_PHYS_OUT_ADDR_EN |
1469 co->codec_nr;
1470
1471 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value);
1472
1473 return 0;
1474 }
1475
1476
1477 static int
1478 auixp_reset_codec(void *aux)
1479 {
1480
1481 /* nothing to be done? */
1482 return 0;
1483 }
1484
1485
1486 static enum ac97_host_flags
1487 auixp_flags_codec(void *aux)
1488 {
1489 struct auixp_codec *ixp_codec;
1490
1491 ixp_codec = aux;
1492 return ixp_codec->codec_flags;
1493 }
1494
1495
1496 static int
1497 auixp_wait_for_codecs(struct auixp_softc *sc, const char *func)
1498 {
1499 bus_space_tag_t iot;
1500 bus_space_handle_t ioh;
1501 uint32_t value;
1502 int timeout;
1503
1504 iot = sc->sc_iot;
1505 ioh = sc->sc_ioh;
1506 /* wait until all codec transfers are done */
1507 timeout = 500; /* 500*2 usec -> 0.001 sec */
1508 do {
1509 value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR);
1510 if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0)
1511 return 0;
1512
1513 DELAY(2);
1514 timeout--;
1515 } while (timeout > 0);
1516
1517 printf("%s: %s: timed out\n", func, device_xname(sc->sc_dev));
1518 return -1;
1519 }
1520
1521
1522
1523 static void
1524 auixp_autodetect_codecs(struct auixp_softc *sc)
1525 {
1526 bus_space_tag_t iot;
1527 bus_space_handle_t ioh;
1528 struct auixp_codec *codec;
1529 int timeout, codec_nr;
1530
1531 iot = sc->sc_iot;
1532 ioh = sc->sc_ioh;
1533 /* ATI IXP can have upto 3 codecs; mark all codecs as not existing */
1534 sc->sc_codec_not_ready_bits = 0;
1535 sc->sc_num_codecs = 0;
1536
1537 /* enable all codecs to interrupt as well as the new frame interrupt */
1538 bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS);
1539
1540 /* wait for the interrupts to happen */
1541 timeout = 100; /* 100.000 usec -> 0.1 sec */
1542
1543 while (timeout > 0) {
1544 DELAY(1000);
1545 if (sc->sc_codec_not_ready_bits)
1546 break;
1547 timeout--;
1548 }
1549
1550 if (timeout == 0)
1551 printf("%s: WARNING: timeout during codec detection; "
1552 "codecs might be present but haven't interrupted\n",
1553 device_xname(sc->sc_dev));
1554
1555 /* disable all interrupts for now */
1556 auixp_disable_interrupts(sc);
1557
1558 /* Attach AC97 host interfaces */
1559 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
1560 codec = &sc->sc_codec[codec_nr];
1561 memset(codec, 0, sizeof(struct auixp_codec));
1562
1563 codec->sc = sc;
1564 codec->codec_nr = codec_nr;
1565 codec->present = 0;
1566
1567 codec->host_if.arg = codec;
1568 codec->host_if.attach = auixp_attach_codec;
1569 codec->host_if.read = auixp_read_codec;
1570 codec->host_if.write = auixp_write_codec;
1571 codec->host_if.reset = auixp_reset_codec;
1572 codec->host_if.flags = auixp_flags_codec;
1573 }
1574
1575 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) {
1576 /* codec 0 present */
1577 DPRINTF(("auixp : YAY! codec 0 present!\n"));
1578 if (ac97_attach(&sc->sc_codec[0].host_if, sc->sc_dev,
1579 &sc->sc_lock) == 0)
1580 sc->sc_num_codecs++;
1581 }
1582
1583 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) {
1584 /* codec 1 present */
1585 DPRINTF(("auixp : YAY! codec 1 present!\n"));
1586 if (ac97_attach(&sc->sc_codec[1].host_if, sc->sc_dev,
1587 &sc->sc_lock) == 0)
1588 sc->sc_num_codecs++;
1589 }
1590
1591 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) {
1592 /* codec 2 present */
1593 DPRINTF(("auixp : YAY! codec 2 present!\n"));
1594 if (ac97_attach(&sc->sc_codec[2].host_if, sc->sc_dev,
1595 &sc->sc_lock) == 0)
1596 sc->sc_num_codecs++;
1597 }
1598
1599 if (sc->sc_num_codecs == 0) {
1600 printf("%s: no codecs detected or "
1601 "no codecs managed to initialise\n",
1602 device_xname(sc->sc_dev));
1603 return;
1604 }
1605
1606 }
1607
1608
1609
1610 /* initialisation routines */
1611
1612 static void
1613 auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1614 {
1615 bus_space_tag_t iot;
1616 bus_space_handle_t ioh;
1617 uint32_t value;
1618
1619 iot = sc->sc_iot;
1620 ioh = sc->sc_ioh;
1621 /* lets not stress the DMA engine more than nessisary */
1622 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1623 if (value & dma->dma_enable_bit) {
1624 value &= ~dma->dma_enable_bit;
1625 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1626 }
1627 }
1628
1629
1630 static void
1631 auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
1632 {
1633 bus_space_tag_t iot;
1634 bus_space_handle_t ioh;
1635 uint32_t value;
1636
1637 iot = sc->sc_iot;
1638 ioh = sc->sc_ioh;
1639 /* lets not stress the DMA engine more than nessisary */
1640 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1641 if (!(value & dma->dma_enable_bit)) {
1642 value |= dma->dma_enable_bit;
1643 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1644 }
1645 }
1646
1647
1648 static void
1649 auixp_reset_aclink(struct auixp_softc *sc)
1650 {
1651 bus_space_tag_t iot;
1652 bus_space_handle_t ioh;
1653 uint32_t value, timeout;
1654
1655 iot = sc->sc_iot;
1656 ioh = sc->sc_ioh;
1657
1658 /* if power is down, power it up */
1659 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1660 if (value & ATI_REG_CMD_POWERDOWN) {
1661 printf("%s: powering up\n", device_xname(sc->sc_dev));
1662
1663 /* explicitly enable power */
1664 value &= ~ATI_REG_CMD_POWERDOWN;
1665 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1666
1667 /* have to wait at least 10 usec for it to initialise */
1668 DELAY(20);
1669 };
1670
1671 printf("%s: soft resetting aclink\n", device_xname(sc->sc_dev));
1672
1673 /* perform a soft reset */
1674 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1675 value |= ATI_REG_CMD_AC_SOFT_RESET;
1676 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1677
1678 /* need to read the CMD reg and wait aprox. 10 usec to init */
1679 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1680 DELAY(20);
1681
1682 /* clear soft reset flag again */
1683 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1684 value &= ~ATI_REG_CMD_AC_SOFT_RESET;
1685 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1686
1687 /* check if the ac-link is working; reset device otherwise */
1688 timeout = 10;
1689 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1690 while (!(value & ATI_REG_CMD_ACLINK_ACTIVE)) {
1691 printf("%s: not up; resetting aclink hardware\n",
1692 device_xname(sc->sc_dev));
1693
1694 /* dip aclink reset but keep the acsync */
1695 value &= ~ATI_REG_CMD_AC_RESET;
1696 value |= ATI_REG_CMD_AC_SYNC;
1697 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1698
1699 /* need to read CMD again and wait again (clocking in issue?) */
1700 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1701 DELAY(20);
1702
1703 /* assert aclink reset again */
1704 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1705 value |= ATI_REG_CMD_AC_RESET;
1706 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1707
1708 /* check if its active now */
1709 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1710
1711 timeout--;
1712 if (timeout == 0) break;
1713 };
1714
1715 if (timeout == 0) {
1716 printf("%s: giving up aclink reset\n", device_xname(sc->sc_dev));
1717 };
1718 if (timeout != 10) {
1719 printf("%s: aclink hardware reset successful\n",
1720 device_xname(sc->sc_dev));
1721 };
1722
1723 /* assert reset and sync for safety */
1724 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1725 value |= ATI_REG_CMD_AC_SYNC | ATI_REG_CMD_AC_RESET;
1726 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1727 }
1728
1729
1730 /* chip hard init */
1731 static int
1732 auixp_init(struct auixp_softc *sc)
1733 {
1734 bus_space_tag_t iot;
1735 bus_space_handle_t ioh;
1736 uint32_t value;
1737
1738 iot = sc->sc_iot;
1739 ioh = sc->sc_ioh;
1740 /* disable all interrupts and clear all sources */
1741 auixp_disable_interrupts(sc);
1742
1743 /* clear all DMA enables (preserving rest of settings) */
1744 value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
1745 value &= ~( ATI_REG_CMD_IN_DMA_EN |
1746 ATI_REG_CMD_OUT_DMA_EN |
1747 ATI_REG_CMD_SPDF_OUT_EN );
1748 bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
1749
1750 /* Reset AC-link */
1751 auixp_reset_aclink(sc);
1752
1753 /*
1754 * codecs get auto-detected later
1755 *
1756 * note: we are NOT enabling interrupts yet, no codecs have been
1757 * detected yet nor is anything else set up
1758 */
1759
1760 return 0;
1761 }
1762
1763 static bool
1764 auixp_resume(device_t dv, const pmf_qual_t *qual)
1765 {
1766 struct auixp_softc *sc = device_private(dv);
1767
1768 mutex_enter(&sc->sc_lock);
1769 auixp_reset_codec(sc);
1770 delay(1000);
1771 (sc->sc_codec[0].codec_if->vtbl->restore_ports)(sc->sc_codec[0].codec_if);
1772 mutex_exit(&sc->sc_lock);
1773
1774 return true;
1775 }
1776
1777 #ifdef DEBUG_AUIXP
1778
1779 static void
1780 auixp_dumpreg(void)
1781 {
1782 struct auixp_softc *sc;
1783 bus_space_tag_t iot;
1784 bus_space_handle_t ioh;
1785 int i;
1786
1787 sc = static_sc;
1788 iot = sc->sc_iot;
1789 ioh = sc->sc_ioh;
1790 printf("%s register dump:\n", device_xname(sc->sc_dev));
1791 for (i = 0; i < 256; i+=4) {
1792 printf("\t0x%02x: 0x%08x\n", i, bus_space_read_4(iot, ioh, i));
1793 }
1794 printf("\n");
1795 }
1796 #endif
1797
1798 static void
1799 auixp_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
1800 {
1801 struct auixp_codec *co = addr;
1802 struct auixp_softc *sc = co->sc;
1803
1804 *intr = &sc->sc_intr_lock;
1805 *proc = &sc->sc_lock;
1806 }
1807