1 /* $NetBSD: audioamd.c,v 1.24 2009/09/17 12:38:11 tsutsui Exp $ */
2 /* NetBSD: am7930_sparc.c,v 1.44 1999/03/14 22:29:00 jonathan Exp */
5 * Copyright (c) 1995 Rolf Grossmann
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Rolf Grossmann.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
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, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: audioamd.c,v 1.24 2009/09/17 12:38:11 tsutsui Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
46 #include <sys/mutex.h>
48 #include <machine/autoconf.h>
50 #include <sys/audioio.h>
51 #include <dev/audio_if.h>
53 #include <dev/ic/am7930reg.h>
54 #include <dev/ic/am7930var.h>
55 #include <sparc/dev/audioamdvar.h>
57 #define AUDIO_ROM_NAME "audio"
60 #define DPRINTF(x) if (am7930debug) printf x
61 #define DPRINTFN(n,x) if (am7930debug>(n)) printf x
65 #endif /* AUDIO_DEBUG */
68 /* interrupt interfaces */
69 int am7930hwintr(void *);
71 void am7930swintr(void *);
73 /* from amd7930intr.s: */
74 void amd7930_trap(void);
77 * interrupt-handler status
79 struct am7930_intrhand
{
80 int (*ih_fun
)(void *);
84 struct audioamd_softc
{
85 struct am7930_softc sc_am7930
; /* glue to MI code */
87 bus_space_tag_t sc_bt
; /* bus cookie */
88 bus_space_handle_t sc_bh
; /* device registers */
90 struct am7930_intrhand sc_ih
; /* interrupt vector (hw or sw) */
91 void (*sc_rintr
)(void*); /* input completion intr handler */
92 void *sc_rarg
; /* arg for sc_rintr() */
93 void (*sc_pintr
)(void*); /* output completion intr handler */
94 void *sc_parg
; /* arg for sc_pintr() */
96 /* sc_au is special in that the hardware interrupt handler uses it */
97 struct auio sc_au
; /* recv and xmit buffers, etc */
98 #define sc_intrcnt sc_au.au_intrcnt /* statistics */
99 void *sc_sicookie
; /* softintr(9) cookie */
103 int audioamd_mainbus_match(device_t
, cfdata_t
, void *);
104 void audioamd_mainbus_attach(device_t
, device_t
, void *);
105 int audioamd_obio_match(device_t
, cfdata_t
, void *);
106 void audioamd_obio_attach(device_t
, device_t
, void *);
107 int audioamd_sbus_match(device_t
, cfdata_t
, void *);
108 void audioamd_sbus_attach(device_t
, device_t
, void *);
109 void audioamd_attach(struct audioamd_softc
*, int);
111 CFATTACH_DECL(audioamd_mainbus
, sizeof(struct audioamd_softc
),
112 audioamd_mainbus_match
, audioamd_mainbus_attach
, NULL
, NULL
);
114 CFATTACH_DECL(audioamd_obio
, sizeof(struct audioamd_softc
),
115 audioamd_obio_match
, audioamd_obio_attach
, NULL
, NULL
);
117 CFATTACH_DECL(audioamd_sbus
, sizeof(struct audioamd_softc
),
118 audioamd_sbus_match
, audioamd_sbus_attach
, NULL
, NULL
);
121 * Define our interface into the am7930 MI driver.
124 uint8_t audioamd_codec_iread(struct am7930_softc
*, int);
125 uint16_t audioamd_codec_iread16(struct am7930_softc
*, int);
126 uint8_t audioamd_codec_dread(struct audioamd_softc
*, int);
127 void audioamd_codec_iwrite(struct am7930_softc
*, int, uint8_t);
128 void audioamd_codec_iwrite16(struct am7930_softc
*, int, uint16_t);
129 void audioamd_codec_dwrite(struct audioamd_softc
*, int, uint8_t);
130 void audioamd_onopen(struct am7930_softc
*);
131 void audioamd_onclose(struct am7930_softc
*);
133 struct am7930_glue audioamd_glue
= {
134 audioamd_codec_iread
,
135 audioamd_codec_iwrite
,
136 audioamd_codec_iread16
,
137 audioamd_codec_iwrite16
,
146 * Define our interface to the higher level audio driver.
148 int audioamd_start_output(void *, void *, int, void (*)(void *), void *);
149 int audioamd_start_input(void *, void *, int, void (*)(void *), void *);
150 int audioamd_getdev(void *, struct audio_device
*);
152 const struct audio_hw_if sa_hw_if
= {
156 am7930_query_encoding
,
158 am7930_round_blocksize
,
159 am7930_commit_settings
,
162 audioamd_start_output
, /* md */
163 audioamd_start_input
, /* md */
171 am7930_query_devinfo
,
182 struct audio_device audioamd_device
= {
190 audioamd_mainbus_match(device_t parent
, cfdata_t cf
, void *aux
)
192 struct mainbus_attach_args
*ma
;
197 return strcmp(AUDIO_ROM_NAME
, ma
->ma_name
) == 0;
201 audioamd_obio_match(device_t parent
, cfdata_t cf
, void *aux
)
203 union obio_attach_args
*uoba
;
206 if (uoba
->uoba_isobio4
!= 0)
209 return strcmp("audio", uoba
->uoba_sbus
.sa_name
) == 0;
213 audioamd_sbus_match(device_t parent
, cfdata_t cf
, void *aux
)
215 struct sbus_attach_args
*sa
;
218 return strcmp(AUDIO_ROM_NAME
, sa
->sa_name
) == 0;
222 audioamd_mainbus_attach(device_t parent
, device_t self
, void *aux
)
224 struct mainbus_attach_args
*ma
;
225 struct audioamd_softc
*sc
;
226 bus_space_handle_t bh
;
229 sc
= device_private(self
);
230 sc
->sc_bt
= ma
->ma_bustag
;
236 BUS_SPACE_MAP_LINEAR
,
238 printf("%s: cannot map registers\n", device_xname(self
));
242 audioamd_attach(sc
, ma
->ma_pri
);
246 audioamd_obio_attach(device_t parent
, device_t self
, void *aux
)
248 union obio_attach_args
*uoba
;
249 struct sbus_attach_args
*sa
;
250 struct audioamd_softc
*sc
;
251 bus_space_handle_t bh
;
254 sa
= &uoba
->uoba_sbus
;
255 sc
= device_private(self
);
256 sc
->sc_bt
= sa
->sa_bustag
;
258 if (sbus_bus_map(sa
->sa_bustag
,
259 sa
->sa_slot
, sa
->sa_offset
,
262 printf("%s: cannot map registers\n", device_xname(self
));
266 audioamd_attach(sc
, sa
->sa_pri
);
270 audioamd_sbus_attach(device_t parent
, device_t self
, void *aux
)
272 struct sbus_attach_args
*sa
;
273 struct audioamd_softc
*sc
;
274 bus_space_handle_t bh
;
277 sc
= device_private(self
);
278 sc
->sc_bt
= sa
->sa_bustag
;
280 if (sbus_bus_map(sa
->sa_bustag
,
281 sa
->sa_slot
, sa
->sa_offset
,
284 printf("%s: cannot map registers\n", device_xname(self
));
288 audioamd_attach(sc
, sa
->sa_pri
);
292 audioamd_attach(struct audioamd_softc
*sc
, int pri
)
297 * Set up glue for MI code early; we use some of it here.
299 self
= &sc
->sc_am7930
.sc_dev
;
300 sc
->sc_am7930
.sc_glue
= &audioamd_glue
;
301 mutex_init(&sc
->sc_lock
, MUTEX_DEFAULT
, IPL_HIGH
);
303 am7930_init(&sc
->sc_am7930
, AUDIOAMD_POLL_MODE
);
307 /* Copy bus tag & handle for use by am7930_trap */
308 sc
->sc_au
.au_bt
= sc
->sc_bt
;
309 sc
->sc_au
.au_bh
= sc
->sc_bh
;
310 (void)bus_intr_establish2(sc
->sc_bt
, pri
, IPL_HIGH
,
312 #ifdef notyet /* XXX amd7930intr.s needs to be fixed for MI softint(9) */
319 sc
->sc_sicookie
= softint_establish(SOFTINT_SERIAL
, am7930swintr
, sc
);
320 if (sc
->sc_sicookie
== NULL
) {
321 printf("\n%s: cannot establish software interrupt\n",
326 printf(" softpri %d\n", IPL_SOFTAUDIO
);
329 evcnt_attach_dynamic(&sc
->sc_intrcnt
, EVCNT_TYPE_INTR
, NULL
,
330 device_xname(self
), "intr");
332 audio_attach_mi(&sa_hw_if
, sc
, self
);
337 audioamd_onopen(struct am7930_softc
*sc
)
339 struct audioamd_softc
*mdsc
;
341 mdsc
= (struct audioamd_softc
*)sc
;
343 /* reset pdma state */
344 mutex_spin_enter(&mdsc
->sc_lock
);
349 mdsc
->sc_au
.au_rdata
= 0;
350 mdsc
->sc_au
.au_pdata
= 0;
351 mutex_spin_exit(&mdsc
->sc_lock
);
356 audioamd_onclose(struct am7930_softc
*sc
)
358 /* On sparc, just do the chipset-level halt. */
359 am7930_halt_input(sc
);
360 am7930_halt_output(sc
);
364 audioamd_start_output(void *addr
, void *p
, int cc
,
365 void (*intr
)(void *), void *arg
)
367 struct audioamd_softc
*sc
;
369 DPRINTFN(1, ("sa_start_output: cc=%d %p (%p)\n", cc
, intr
, arg
));
372 mutex_spin_enter(&sc
->sc_lock
);
373 audioamd_codec_iwrite(&sc
->sc_am7930
,
374 AM7930_IREG_INIT
, AM7930_INIT_PMS_ACTIVE
);
377 sc
->sc_au
.au_pdata
= p
;
378 sc
->sc_au
.au_pend
= (char *)p
+ cc
- 1;
379 mutex_spin_exit(&sc
->sc_lock
);
381 DPRINTF(("sa_start_output: started intrs.\n"));
386 audioamd_start_input(void *addr
, void *p
, int cc
,
387 void (*intr
)(void *), void *arg
)
389 struct audioamd_softc
*sc
;
391 DPRINTFN(1, ("sa_start_input: cc=%d %p (%p)\n", cc
, intr
, arg
));
394 mutex_spin_enter(&sc
->sc_lock
);
395 audioamd_codec_iwrite(&sc
->sc_am7930
,
396 AM7930_IREG_INIT
, AM7930_INIT_PMS_ACTIVE
);
399 sc
->sc_au
.au_rdata
= p
;
400 sc
->sc_au
.au_rend
= (char *)p
+ cc
-1;
401 mutex_spin_exit(&sc
->sc_lock
);
403 DPRINTF(("sa_start_input: started intrs.\n"));
410 * Pseudo-DMA support: either C or locore assember.
414 am7930hwintr(void *v
)
416 struct audioamd_softc
*sc
;
423 mutex_spin_enter(&sc
->sc_lock
);
425 /* clear interrupt */
426 k
= audioamd_codec_dread(sc
, AM7930_DREG_IR
);
427 if ((k
& (AM7930_IR_DTTHRSH
|AM7930_IR_DRTHRSH
|AM7930_IR_DSRI
|
428 AM7930_IR_DERI
|AM7930_IR_BBUFF
)) == 0) {
429 mutex_spin_exit(&sc
->sc_lock
);
433 /* receive incoming data */
437 *d
= audioamd_codec_dread(sc
, AM7930_DREG_BBRB
);
440 DPRINTFN(1, ("am7930hwintr: swintr(r) requested"));
441 softint_schedule(sc
->sc_sicookie
);
445 /* send outgoing data */
449 audioamd_codec_dwrite(sc
, AM7930_DREG_BBTB
, *d
);
452 DPRINTFN(1, ("am7930hwintr: swintr(p) requested"));
453 softint_schedule(sc
->sc_sicookie
);
457 au
->au_intrcnt
.ev_count
++;
458 mutex_spin_exit(&sc
->sc_lock
);
464 am7930swintr(void *sc0
)
466 struct audioamd_softc
*sc
;
471 DPRINTFN(1, ("audiointr: sc=%p\n", sc
););
474 mutex_spin_enter(&sc
->sc_lock
);
475 if (au
->au_rdata
> au
->au_rend
&& sc
->sc_rintr
!= NULL
) {
476 mutex_spin_exit(&sc
->sc_lock
);
477 (*sc
->sc_rintr
)(sc
->sc_rarg
);
478 mutex_spin_enter(&sc
->sc_lock
);
480 pint
= (au
->au_pdata
> au
->au_pend
&& sc
->sc_pintr
!= NULL
);
481 mutex_spin_exit(&sc
->sc_lock
);
483 (*sc
->sc_pintr
)(sc
->sc_parg
);
489 audioamd_codec_iwrite(struct am7930_softc
*sc
, int reg
, uint8_t val
)
491 struct audioamd_softc
*mdsc
;
493 mdsc
= (struct audioamd_softc
*)sc
;
494 audioamd_codec_dwrite(mdsc
, AM7930_DREG_CR
, reg
);
495 audioamd_codec_dwrite(mdsc
, AM7930_DREG_DR
, val
);
499 audioamd_codec_iwrite16(struct am7930_softc
*sc
, int reg
, uint16_t val
)
501 struct audioamd_softc
*mdsc
;
503 mdsc
= (struct audioamd_softc
*)sc
;
504 audioamd_codec_dwrite(mdsc
, AM7930_DREG_CR
, reg
);
505 audioamd_codec_dwrite(mdsc
, AM7930_DREG_DR
, val
);
506 audioamd_codec_dwrite(mdsc
, AM7930_DREG_DR
, val
>>8);
512 audioamd_codec_iread(struct am7930_softc
*sc
, int reg
)
514 struct audioamd_softc
*mdsc
;
516 mdsc
= (struct audioamd_softc
*)sc
;
517 audioamd_codec_dwrite(mdsc
, AM7930_DREG_CR
, reg
);
518 return (audioamd_codec_dread(mdsc
, AM7930_DREG_DR
));
522 audioamd_codec_iread16(struct am7930_softc
*sc
, int reg
)
524 struct audioamd_softc
*mdsc
;
527 mdsc
= (struct audioamd_softc
*)sc
;
528 audioamd_codec_dwrite(mdsc
, AM7930_DREG_CR
, reg
);
529 lo
= audioamd_codec_dread(mdsc
, AM7930_DREG_DR
);
530 hi
= audioamd_codec_dread(mdsc
, AM7930_DREG_DR
);
531 return (hi
<< 8) | lo
;
536 audioamd_codec_dread(struct audioamd_softc
*sc
, int reg
)
539 return bus_space_read_1(sc
->sc_bt
, sc
->sc_bh
, reg
);
544 audioamd_codec_dwrite(struct audioamd_softc
*sc
, int reg
, uint8_t val
)
547 bus_space_write_1(sc
->sc_bt
, sc
->sc_bh
, reg
, val
);
551 audioamd_getdev(void *addr
, struct audio_device
*retp
)
554 *retp
= audioamd_device
;
558 #endif /* NAUDIO > 0 */