#include <sys/param.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/audioio.h>
#include <sys/queue.h>
#include <machine/bus.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pcivar.h>
#include <dev/audio_if.h>
#include <dev/ic/ac97.h>
#include <dev/pci/auixpreg.h>
#include <dev/pci/auixpvar.h>
#define ALL_CODECS_NOT_READY \
(ATI_REG_ISR_CODEC0_NOT_READY | ATI_REG_ISR_CODEC1_NOT_READY |\
ATI_REG_ISR_CODEC2_NOT_READY)
#define CODEC_CHECK_BITS (ALL_CODECS_NOT_READY|ATI_REG_ISR_NEW_FRAME)
#define PCI_CBIO 0x10
#define KERNADDR(p) ((void *)((p)->addr))
#define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
const struct pci_matchid auixp_pci_devices[] = {
{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB200_AUDIO },
{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB300_AUDIO },
{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB400_AUDIO },
{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_AUDIO }
};
struct cfdriver auixp_cd = {
NULL, "auixp", DV_DULL
};
int auixp_match( struct device *, void *, void *);
void auixp_attach(struct device *, struct device *, void *);
int auixp_detach(struct device *, int);
int auixp_activate(struct device *, int);
const struct cfattach auixp_ca = {
sizeof(struct auixp_softc), auixp_match, auixp_attach,
NULL, auixp_activate
};
int auixp_open(void *v, int flags);
void auixp_close(void *v);
int auixp_set_params(void *, int, int, struct audio_params *,
struct audio_params *);
int auixp_commit_settings(void *);
int auixp_round_blocksize(void *, int);
int auixp_trigger_output(void *, void *, void *, int,
void (*)(void *), void *, struct audio_params *);
int auixp_trigger_input(void *, void *, void *, int,
void (*)(void *), void *, struct audio_params *);
int auixp_halt_output(void *);
int auixp_halt_input(void *);
int auixp_set_port(void *, mixer_ctrl_t *);
int auixp_get_port(void *, mixer_ctrl_t *);
int auixp_query_devinfo(void *, mixer_devinfo_t *);
void * auixp_malloc(void *, int, size_t, int, int);
void auixp_free(void *, void *, int);
int auixp_intr(void *);
int auixp_allocmem(struct auixp_softc *, size_t, size_t,
struct auixp_dma *);
int auixp_freemem(struct auixp_softc *, struct auixp_dma *);
int auixp_init(struct auixp_softc *);
void auixp_autodetect_codecs(struct auixp_softc *);
void auixp_post_config(struct device *);
void auixp_reset_aclink(struct auixp_softc *);
int auixp_attach_codec(void *, struct ac97_codec_if *);
int auixp_read_codec(void *, u_int8_t, u_int16_t *);
int auixp_write_codec(void *, u_int8_t, u_int16_t);
int auixp_wait_for_codecs(struct auixp_softc *, const char *);
void auixp_reset_codec(void *);
enum ac97_host_flags auixp_flags_codec(void *);
void auixp_enable_dma(struct auixp_softc *, struct auixp_dma *);
void auixp_disable_dma(struct auixp_softc *, struct auixp_dma *);
void auixp_enable_interrupts(struct auixp_softc *);
void auixp_disable_interrupts(struct auixp_softc *);
void auixp_link_daisychain(struct auixp_softc *,
struct auixp_dma *, struct auixp_dma *, int, int);
int auixp_allocate_dma_chain(struct auixp_softc *, struct auixp_dma **);
void auixp_program_dma_chain(struct auixp_softc *, struct auixp_dma *);
void auixp_dma_update(struct auixp_softc *, struct auixp_dma *);
void auixp_update_busbusy(struct auixp_softc *);
#ifdef DEBUG_AUIXP
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
const struct audio_hw_if auixp_hw_if = {
.open = auixp_open,
.close = auixp_close,
.set_params = auixp_set_params,
.round_blocksize = auixp_round_blocksize,
.commit_settings = auixp_commit_settings,
.halt_output = auixp_halt_output,
.halt_input = auixp_halt_input,
.set_port = auixp_set_port,
.get_port = auixp_get_port,
.query_devinfo = auixp_query_devinfo,
.allocm = auixp_malloc,
.freem = auixp_free,
.trigger_output = auixp_trigger_output,
.trigger_input = auixp_trigger_input,
};
int
auixp_open(void *v, int flags)
{
return 0;
}
void
auixp_close(void *v)
{
}
int
auixp_commit_settings(void *hdl)
{
struct auixp_codec *co;
struct auixp_softc *sc;
bus_space_tag_t iot;
bus_space_handle_t ioh;
struct audio_params *params;
u_int32_t value;
co = (struct auixp_codec *) hdl;
sc = co->sc;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
params = &sc->sc_play_params;
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value &= ~ATI_REG_CMD_INTERLEAVE_IN;
if (params->precision <= 16)
value |= ATI_REG_CMD_INTERLEAVE_IN;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
params = &sc->sc_play_params;
value = bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT);
value &= ~ATI_REG_OUT_DMA_SLOT_MASK;
switch (params->channels) {
case 6:
value |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
ATI_REG_OUT_DMA_SLOT_BIT(8);
case 4:
value |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
ATI_REG_OUT_DMA_SLOT_BIT(9);
default:
value |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
ATI_REG_OUT_DMA_SLOT_BIT(4);
break;
}
value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value &= ~ATI_REG_CMD_INTERLEAVE_OUT;
if (params->precision <= 16)
value |= ATI_REG_CMD_INTERLEAVE_OUT;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
value = bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER);
value &= ~ATI_REG_6CH_REORDER_EN;
if (params->channels == 6)
value |= ATI_REG_6CH_REORDER_EN;
bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value);
if (sc->has_spdif) {
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK;
value |= ATI_REG_CMD_SPDF_CONFIG_34;
value &= ~ATI_REG_CMD_INTERLEAVE_SPDF;
if (params->precision <= 16)
value |= ATI_REG_CMD_INTERLEAVE_SPDF;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
}
return 0;
}
int
auixp_set_params(void *hdl, int setmode, int usemode,
struct audio_params *play, struct audio_params *rec)
{
struct auixp_codec *co;
int error;
u_int temprate;
co = (struct auixp_codec *) hdl;
if (setmode & AUMODE_PLAY) {
play->channels = 2;
play->precision = 16;
switch(play->encoding) {
case AUDIO_ENCODING_SLINEAR_LE:
break;
default:
return (EINVAL);
}
play->bps = AUDIO_BPS(play->precision);
play->msb = 1;
temprate = play->sample_rate;
error = ac97_set_rate(co->codec_if,
AC97_REG_PCM_LFE_DAC_RATE, &play->sample_rate);
if (error)
return (error);
play->sample_rate = temprate;
error = ac97_set_rate(co->codec_if,
AC97_REG_PCM_SURR_DAC_RATE, &play->sample_rate);
if (error)
return (error);
play->sample_rate = temprate;
error = ac97_set_rate(co->codec_if,
AC97_REG_PCM_FRONT_DAC_RATE, &play->sample_rate);
if (error)
return (error);
}
if (setmode & AUMODE_RECORD) {
rec->channels = 2;
rec->precision = 16;
switch(rec->encoding) {
case AUDIO_ENCODING_SLINEAR_LE:
break;
default:
return (EINVAL);
}
rec->bps = AUDIO_BPS(rec->precision);
rec->msb = 1;
error = ac97_set_rate(co->codec_if, AC97_REG_PCM_LR_ADC_RATE,
&rec->sample_rate);
if (error)
return (error);
}
return (0);
}
int
auixp_round_blocksize(void *v, int blk)
{
blk = (blk + 0x1f) & ~0x1f;
if (blk > 0x10000)
blk = 0x10000;
return blk;
}
void *
auixp_malloc(void *hdl, int direction, size_t size, int pool, int flags)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *dma;
int error;
co = (struct auixp_codec *) hdl;
sc = co->sc;
dma = malloc(sizeof(*dma), pool, flags);
if (!dma)
return NULL;
error = auixp_allocmem(sc, size, 16, dma);
if (error) {
free(dma, pool, sizeof(*dma));
printf("%s: auixp_malloc: not enough memory\n",
sc->sc_dev.dv_xname);
return NULL;
}
SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain);
DPRINTF(("auixp_malloc: returning kern %p, hw 0x%08x for %zu bytes "
"in %d segs\n", KERNADDR(dma), (u_int32_t) DMAADDR(dma), dma->size,
dma->nsegs)
);
return KERNADDR(dma);
}
void
auixp_free(void *hdl, void *addr, int pool)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *dma;
co = (struct auixp_codec *) hdl;
sc = co->sc;
SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) {
if (KERNADDR(dma) == addr) {
SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma,
dma_chain);
auixp_freemem(sc, dma);
free(dma, pool, sizeof(*dma));
return;
}
}
}
int
auixp_set_port(void *hdl, mixer_ctrl_t *mc)
{
struct auixp_codec *co;
co = (struct auixp_codec *) hdl;
return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc);
}
int
auixp_get_port(void *hdl, mixer_ctrl_t *mc)
{
struct auixp_codec *co;
co = (struct auixp_codec *) hdl;
return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc);
}
int
auixp_query_devinfo(void *hdl, mixer_devinfo_t *di)
{
struct auixp_codec *co;
co = (struct auixp_codec *) hdl;
return co->codec_if->vtbl->query_devinfo(co->codec_if, di);
}
void
auixp_link_daisychain(struct auixp_softc *sc,
struct auixp_dma *c_dma, struct auixp_dma *s_dma,
int blksize, int blocks)
{
atiixp_dma_desc_t *caddr_v, *next_caddr_v;
u_int32_t caddr_p, next_caddr_p, saddr_p;
int i;
auixp_disable_dma(sc, c_dma);
caddr_v = KERNADDR(c_dma);
caddr_p = DMAADDR(c_dma);
saddr_p = DMAADDR(s_dma);
for (i = 0; i < blocks; i++) {
bzero(caddr_v, sizeof(atiixp_dma_desc_t));
next_caddr_v = caddr_v + 1;
next_caddr_p = caddr_p + sizeof(atiixp_dma_desc_t);
if (i == blocks-1) {
next_caddr_v = KERNADDR(c_dma);
next_caddr_p = DMAADDR(c_dma);
}
caddr_v->addr = htole32(saddr_p);
caddr_v->status = htole16(0);
caddr_v->size = htole16((blksize >> 2));
caddr_v->next = htole32(next_caddr_p);
saddr_p += blksize;
caddr_v = next_caddr_v;
caddr_p = next_caddr_p;
}
}
int
auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap)
{
struct auixp_dma *dma;
int error;
*dmap = NULL;
dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT | M_ZERO);
if (!dma)
return ENOMEM;
error = auixp_allocmem(sc, DMA_DESC_CHAIN * sizeof(atiixp_dma_desc_t),
16, dma);
if (error) {
printf("%s: can't malloc dma descriptor chain\n",
sc->sc_dev.dv_xname);
free(dma, M_DEVBUF, sizeof(*dma));
return ENOMEM;
}
dma->intr = NULL;
dma->intrarg = NULL;
*dmap = dma;
return 0;
}
void
auixp_program_dma_chain(struct auixp_softc *sc, struct auixp_dma *dma)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
value = DMAADDR(dma);
value = value | ATI_REG_LINKPTR_EN;
bus_space_write_4(iot, ioh, dma->linkptr, 0);
auixp_disable_dma(sc, dma);
auixp_enable_dma(sc, dma);
bus_space_write_4(iot, ioh, dma->linkptr, value);
}
void
auixp_dma_update(struct auixp_softc *sc, struct auixp_dma *dma)
{
if (!dma)
panic("auixp: update: dma = NULL");
if (!dma->intr)
panic("auixp: update: dma->intr = NULL");
(*dma->intr)(dma->intrarg);
}
void
auixp_update_busbusy(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
int running;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
value = bus_space_read_4(iot, ioh, ATI_REG_IER);
value &= ~ATI_REG_IER_SET_BUS_BUSY;
running = ((sc->sc_output_dma->running) || (sc->sc_input_dma->running));
if (running)
value |= ATI_REG_IER_SET_BUS_BUSY;
bus_space_write_4(iot, ioh, ATI_REG_IER, value);
}
int
auixp_trigger_output(void *hdl, void *start, void *end, int blksize,
void (*intr)(void *), void *intrarg, struct audio_params *param)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *chain_dma;
struct auixp_dma *sound_dma;
u_int32_t blocks;
co = (struct auixp_codec *) hdl;
sc = co->sc;
chain_dma = sc->sc_output_dma;
chain_dma->intr = intr;
chain_dma->intrarg = intrarg;
blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
if (KERNADDR(sound_dma) == start)
break;
}
if (!sound_dma) {
printf("%s: auixp_trigger_output: bad sound addr %p\n",
sc->sc_dev.dv_xname, start);
return EINVAL;
}
auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
auixp_program_dma_chain(sc, chain_dma);
mtx_enter(&audio_lock);
chain_dma->running = 1;
auixp_update_busbusy(sc);
mtx_leave(&audio_lock);
return 0;
}
int
auixp_halt_output(void *hdl)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *dma;
mtx_enter(&audio_lock);
co = (struct auixp_codec *) hdl;
sc = co->sc;
dma = sc->sc_output_dma;
auixp_disable_dma(sc, dma);
dma->running = 0;
auixp_update_busbusy(sc);
mtx_leave(&audio_lock);
return 0;
}
int
auixp_trigger_input(void *hdl, void *start, void *end, int blksize,
void (*intr)(void *), void *intrarg, struct audio_params *param)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *chain_dma;
struct auixp_dma *sound_dma;
u_int32_t blocks;
co = (struct auixp_codec *) hdl;
sc = co->sc;
chain_dma = sc->sc_input_dma;
chain_dma->intr = intr;
chain_dma->intrarg = intrarg;
blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
if (KERNADDR(sound_dma) == start)
break;
}
if (!sound_dma) {
printf("%s: auixp_trigger_input: bad sound addr %p\n",
sc->sc_dev.dv_xname, start);
return EINVAL;
}
auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
auixp_program_dma_chain(sc, chain_dma);
mtx_enter(&audio_lock);
chain_dma->running = 1;
auixp_update_busbusy(sc);
mtx_leave(&audio_lock);
return 0;
}
int
auixp_halt_input(void *hdl)
{
struct auixp_codec *co;
struct auixp_softc *sc;
struct auixp_dma *dma;
mtx_enter(&audio_lock);
co = (struct auixp_codec *) hdl;
sc = co->sc;
dma = sc->sc_input_dma;
auixp_disable_dma(sc, dma);
dma->running = 0;
auixp_update_busbusy(sc);
mtx_leave(&audio_lock);
return 0;
}
int
auixp_intr(void *softc)
{
struct auixp_softc *sc;
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t status, enable, detected_codecs;
int ret;
mtx_enter(&audio_lock);
sc = softc;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
ret = 0;
status = bus_space_read_4(iot, ioh, ATI_REG_ISR);
if (status == 0) {
mtx_leave(&audio_lock);
return 0;
}
DPRINTF(("%s: (status = %x)\n", sc->sc_dev.dv_xname, status));
if (status & ATI_REG_ISR_IN_STATUS) {
ret++; DPRINTF(("IN_STATUS\n"));
auixp_dma_update(sc, sc->sc_input_dma);
}
if (status & ATI_REG_ISR_OUT_STATUS) {
ret++; DPRINTF(("OUT_STATUS\n"));
auixp_dma_update(sc, sc->sc_output_dma);
}
if (status & ATI_REG_ISR_IN_XRUN) {
ret++; DPRINTF(("IN_XRUN\n"));
}
if (status & ATI_REG_ISR_OUT_XRUN) {
ret++; DPRINTF(("OUT_XRUN\n"));
}
if (status & CODEC_CHECK_BITS) {
ret++;
detected_codecs = status & CODEC_CHECK_BITS;
sc->sc_codec_not_ready_bits |= detected_codecs;
enable = bus_space_read_4(iot, ioh, ATI_REG_IER);
enable &= ~detected_codecs;
bus_space_write_4(iot, ioh, ATI_REG_IER, enable);
}
bus_space_write_4(iot, ioh, ATI_REG_ISR, status);
mtx_leave(&audio_lock);
return ret;
}
int
auixp_allocmem(struct auixp_softc *sc, size_t size,
size_t align, struct auixp_dma *dma)
{
int error;
dma->size = size;
error = bus_dmamem_alloc(sc->sc_dmat, dma->size, align, 0,
dma->segs, sizeof(dma->segs) / sizeof(dma->segs[0]), &dma->nsegs,
BUS_DMA_NOWAIT);
if (error)
return error;
error = bus_dmamem_map(sc->sc_dmat, dma->segs, dma->nsegs, dma->size,
&dma->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
if (error)
goto free;
error = bus_dmamap_create(sc->sc_dmat, dma->size, 1, dma->size, 0,
BUS_DMA_NOWAIT, &dma->map);
if (error)
goto unmap;
error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->addr, dma->size, NULL,
BUS_DMA_NOWAIT);
if (error)
goto destroy;
return 0;
destroy:
bus_dmamap_destroy(sc->sc_dmat, dma->map);
unmap:
bus_dmamem_unmap(sc->sc_dmat, dma->addr, dma->size);
free:
bus_dmamem_free(sc->sc_dmat, dma->segs, dma->nsegs);
return error;
}
int
auixp_freemem(struct auixp_softc *sc, struct auixp_dma *p)
{
bus_dmamap_unload(sc->sc_dmat, p->map);
bus_dmamap_destroy(sc->sc_dmat, p->map);
bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
return 0;
}
int
auixp_match(struct device *dev, void *match, void *aux)
{
return (pci_matchbyid((struct pci_attach_args *)aux, auixp_pci_devices,
sizeof(auixp_pci_devices)/sizeof(auixp_pci_devices[0])));
}
int
auixp_activate(struct device *self, int act)
{
struct auixp_softc *sc = (struct auixp_softc *)self;
int rv = 0;
switch (act) {
case DVACT_SUSPEND:
rv = config_activate_children(self, act);
auixp_disable_interrupts(sc);
break;
case DVACT_RESUME:
auixp_init(sc);
ac97_resume(&sc->sc_codec.host_if, sc->sc_codec.codec_if);
rv = config_activate_children(self, act);
break;
default:
rv = config_activate_children(self, act);
break;
}
return (rv);
}
void
auixp_attach(struct device *parent, struct device *self, void *aux)
{
struct auixp_softc *sc;
struct pci_attach_args *pa;
pcitag_t tag;
pci_chipset_tag_t pc;
pci_intr_handle_t ih;
const char *intrstr;
sc = (struct auixp_softc *)self;
pa = (struct pci_attach_args *)aux;
tag = pa->pa_tag;
pc = pa->pa_pc;
if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_MEM, 0,
&sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) {
printf(": can't map mem space\n");
return;
}
sc->sc_tag = tag;
sc->sc_pct = pc;
sc->sc_dmat = pa->pa_dmat;
SLIST_INIT(&sc->sc_dma_list);
auixp_allocate_dma_chain(sc, &sc->sc_output_dma);
auixp_allocate_dma_chain(sc, &sc->sc_input_dma);
if (!sc->sc_output_dma || !sc->sc_input_dma)
return;
#if 0
auixp_program_dma_chain(sc, sc->sc_output_dma);
auixp_program_dma_chain(sc, sc->sc_input_dma);
#endif
if (pci_intr_map(pa, &ih)) {
printf(": can't map interrupt\n");
return;
}
intrstr = pci_intr_string(pc, ih);
sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE,
auixp_intr, sc, sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": can't establish interrupt");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
return;
}
printf(": %s\n", intrstr);
pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0);
if (auixp_init(sc) == -1) {
printf("%s: auixp_attach: unable to initialize the card\n",
sc->sc_dev.dv_xname);
return;
}
config_mountroot(self, auixp_post_config);
}
void
auixp_post_config(struct device *self)
{
struct auixp_softc *sc = (struct auixp_softc *)self;
auixp_autodetect_codecs(sc);
if (!sc->sc_codec.present) {
printf("%s: no codecs detected or initialised\n",
sc->sc_dev.dv_xname);
return;
}
audio_attach_mi(&auixp_hw_if, &sc->sc_codec, NULL, &sc->sc_dev);
if (sc->has_spdif)
sc->has_spdif = 0;
sc->sc_output_dma->linkptr = ATI_REG_OUT_DMA_LINKPTR;
sc->sc_output_dma->dma_enable_bit = ATI_REG_CMD_OUT_DMA_EN |
ATI_REG_CMD_SEND_EN;
if (sc->has_spdif)
sc->sc_output_dma->dma_enable_bit |= ATI_REG_CMD_SPDF_OUT_EN;
sc->sc_input_dma->linkptr = ATI_REG_IN_DMA_LINKPTR;
sc->sc_input_dma->dma_enable_bit = ATI_REG_CMD_IN_DMA_EN |
ATI_REG_CMD_RECEIVE_EN;
auixp_enable_interrupts(sc);
}
void
auixp_enable_interrupts(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
value = bus_space_read_4(iot, ioh, ATI_REG_IER);
value |= ATI_REG_IER_IO_STATUS_EN;
bus_space_write_4(iot, ioh, ATI_REG_IER, value);
}
void
auixp_disable_interrupts(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_4(iot, ioh, ATI_REG_IER, 0);
bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
}
int
auixp_detach(struct device *self, int flags)
{
struct auixp_softc *sc;
sc = (struct auixp_softc *)self;
auixp_disable_interrupts(sc);
config_detach(&sc->sc_dev, flags);
if (sc->sc_ih != NULL)
pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
if (sc->sc_ios)
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
return 0;
}
int
auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if)
{
struct auixp_codec *ixp_codec;
ixp_codec = aux;
ixp_codec->codec_if = codec_if;
return 0;
}
int
auixp_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
{
struct auixp_codec *co;
struct auixp_softc *sc;
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t data;
int timeout;
co = aux;
sc = co->sc;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
if (auixp_wait_for_codecs(sc, "read_codec"))
return 0xffff;
data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
ATI_REG_PHYS_OUT_ADDR_EN |
ATI_REG_PHYS_OUT_RW |
co->codec_nr;
bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data);
if (auixp_wait_for_codecs(sc, "read_codec"))
return 0xffff;
timeout = 500;
do {
data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR);
if (data & ATI_REG_PHYS_IN_READ_FLAG) {
DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n",
reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT));
*result = data >> ATI_REG_PHYS_IN_DATA_SHIFT;
return 0;
}
DELAY(2);
timeout--;
} while (timeout > 0);
if (reg < 0x7c)
printf("%s: codec read timeout! (reg %x)\n",
sc->sc_dev.dv_xname, reg);
return 0xffff;
}
int
auixp_write_codec(void *aux, u_int8_t reg, u_int16_t data)
{
struct auixp_codec *co;
struct auixp_softc *sc;
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data));
co = aux;
sc = co->sc;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
if (auixp_wait_for_codecs(sc, "write_codec"))
return -1;
value = (((u_int32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) |
(((u_int32_t) reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
ATI_REG_PHYS_OUT_ADDR_EN |
co->codec_nr;
bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value);
return 0;
}
void
auixp_reset_codec(void *aux)
{
}
enum ac97_host_flags
auixp_flags_codec(void *aux)
{
struct auixp_codec *ixp_codec;
ixp_codec = aux;
return ixp_codec->codec_flags;
}
int
auixp_wait_for_codecs(struct auixp_softc *sc, const char *func)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
int timeout;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
timeout = 500;
do {
value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR);
if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0)
return 0;
DELAY(2);
timeout--;
} while (timeout > 0);
printf("%s: %s: timed out\n", func, sc->sc_dev.dv_xname);
return -1;
}
void
auixp_autodetect_codecs(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
pcireg_t subdev;
struct auixp_codec *codec;
int timeout;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
subdev = pci_conf_read(sc->sc_pct, sc->sc_tag, PCI_SUBSYS_ID_REG);
sc->sc_codec_not_ready_bits = 0;
bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS);
timeout = 100;
while (timeout > 0) {
DELAY(1000);
if (sc->sc_codec_not_ready_bits)
break;
timeout--;
}
if (timeout == 0)
printf("%s: WARNING: timeout during codec detection; "
"codecs might be present but haven't interrupted\n",
sc->sc_dev.dv_xname);
auixp_disable_interrupts(sc);
codec = &sc->sc_codec;
bzero(codec, sizeof(struct auixp_codec));
codec->sc = sc;
codec->host_if.arg = codec;
codec->host_if.attach = auixp_attach_codec;
codec->host_if.read = auixp_read_codec;
codec->host_if.write = auixp_write_codec;
codec->host_if.reset = auixp_reset_codec;
codec->host_if.flags = auixp_flags_codec;
switch (subdev) {
case 0x1311462:
case 0x1611462:
case 0x3511462:
case 0x4711462:
case 0x0611462:
codec->codec_flags = AC97_HOST_ALC650_PIN47_IS_EAPD;
break;
}
if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) {
DPRINTF(("auixp : YAY! codec 0 present!\n"));
if (ac97_attach(&sc->sc_codec.host_if) == 0) {
sc->sc_codec.codec_nr = 0;
sc->sc_codec.present = 1;
return;
}
}
if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) {
DPRINTF(("auixp : YAY! codec 1 present!\n"));
if (ac97_attach(&sc->sc_codec.host_if) == 0) {
sc->sc_codec.codec_nr = 1;
sc->sc_codec.present = 1;
return;
}
}
if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) {
DPRINTF(("auixp : YAY! codec 2 present!\n"));
if (ac97_attach(&sc->sc_codec.host_if) == 0) {
sc->sc_codec.codec_nr = 2;
sc->sc_codec.present = 1;
return;
}
}
}
void
auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
if (value & dma->dma_enable_bit) {
value &= ~dma->dma_enable_bit;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
}
}
void
auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
if (!(value & dma->dma_enable_bit)) {
value |= dma->dma_enable_bit;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
}
}
void
auixp_reset_aclink(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value, timeout;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
if (value & ATI_REG_CMD_POWERDOWN) {
printf("%s: powering up\n", sc->sc_dev.dv_xname);
value &= ~ATI_REG_CMD_POWERDOWN;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
DELAY(20);
}
printf("%s: soft resetting aclink\n", sc->sc_dev.dv_xname);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value |= ATI_REG_CMD_AC_SOFT_RESET;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
DELAY(20);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value &= ~ATI_REG_CMD_AC_SOFT_RESET;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
timeout = 10;
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
while (!(value & ATI_REG_CMD_ACLINK_ACTIVE)) {
printf("%s: not up; resetting aclink hardware\n",
sc->sc_dev.dv_xname);
value &= ~ATI_REG_CMD_AC_RESET;
value |= ATI_REG_CMD_AC_SYNC;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
DELAY(20);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value |= ATI_REG_CMD_AC_RESET;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
timeout--;
if (timeout == 0) break;
}
if (timeout == 0) {
printf("%s: giving up aclink reset\n", sc->sc_dev.dv_xname);
}
if (timeout != 10) {
printf("%s: aclink hardware reset successful\n",
sc->sc_dev.dv_xname);
}
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value |= ATI_REG_CMD_AC_SYNC | ATI_REG_CMD_AC_RESET;
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
}
int
auixp_init(struct auixp_softc *sc)
{
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int32_t value;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
auixp_disable_interrupts(sc);
value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
value &= ~( ATI_REG_CMD_IN_DMA_EN |
ATI_REG_CMD_OUT_DMA_EN |
ATI_REG_CMD_SPDF_OUT_EN );
bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
auixp_reset_aclink(sc);
return 0;
}