1987e5972SCameron Grant /* 2987e5972SCameron Grant * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3987e5972SCameron Grant * All rights reserved. 4987e5972SCameron Grant * 5987e5972SCameron Grant * Redistribution and use in source and binary forms, with or without 6987e5972SCameron Grant * modification, are permitted provided that the following conditions 7987e5972SCameron Grant * are met: 8987e5972SCameron Grant * 1. Redistributions of source code must retain the above copyright 9987e5972SCameron Grant * notice, this list of conditions and the following disclaimer. 10987e5972SCameron Grant * 2. Redistributions in binary form must reproduce the above copyright 11987e5972SCameron Grant * notice, this list of conditions and the following disclaimer in the 12987e5972SCameron Grant * documentation and/or other materials provided with the distribution. 13987e5972SCameron Grant * 14987e5972SCameron Grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15987e5972SCameron Grant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16987e5972SCameron Grant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17987e5972SCameron Grant * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18987e5972SCameron Grant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19987e5972SCameron Grant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20987e5972SCameron Grant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21987e5972SCameron Grant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 22987e5972SCameron Grant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23987e5972SCameron Grant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF 24987e5972SCameron Grant * SUCH DAMAGE. 25987e5972SCameron Grant * 2653c5a968SPeter Wemm * $FreeBSD$ 27987e5972SCameron Grant */ 28987e5972SCameron Grant 29ef9308b1SCameron Grant #include <dev/sound/pcm/sound.h> 30ef9308b1SCameron Grant #include <dev/sound/pcm/ac97.h> 31ef9308b1SCameron Grant #include <dev/sound/pci/t4dwave.h> 32987e5972SCameron Grant 33987e5972SCameron Grant #include <pci/pcireg.h> 34987e5972SCameron Grant #include <pci/pcivar.h> 35987e5972SCameron Grant 36987e5972SCameron Grant /* -------------------------------------------------------------------- */ 37987e5972SCameron Grant 38dedf4424SCameron Grant #define TDX_PCI_ID 0x20001023 39dedf4424SCameron Grant #define TNX_PCI_ID 0x20011023 40dedf4424SCameron Grant 41dedf4424SCameron Grant #define TR_BUFFSIZE 0xf000 42dedf4424SCameron Grant #define TR_TIMEOUT_CDC 0xffff 43dedf4424SCameron Grant #define TR_INTSAMPLES 0x2000 44dedf4424SCameron Grant #define TR_MAXPLAYCH 4 45dedf4424SCameron Grant 46987e5972SCameron Grant struct tr_info; 47987e5972SCameron Grant 48987e5972SCameron Grant /* channel registers */ 49987e5972SCameron Grant struct tr_chinfo { 50987e5972SCameron Grant u_int32_t cso, alpha, fms, fmc, ec; 51987e5972SCameron Grant u_int32_t lba; 52987e5972SCameron Grant u_int32_t eso, delta; 53987e5972SCameron Grant u_int32_t rvol, cvol; 54987e5972SCameron Grant u_int32_t gvsel, pan, vol, ctrl; 55987e5972SCameron Grant int index; 56987e5972SCameron Grant snd_dbuf *buffer; 57987e5972SCameron Grant pcm_channel *channel; 58987e5972SCameron Grant struct tr_info *parent; 59987e5972SCameron Grant }; 60987e5972SCameron Grant 61987e5972SCameron Grant /* device private data */ 62987e5972SCameron Grant struct tr_info { 63987e5972SCameron Grant u_int32_t type; 64987e5972SCameron Grant 65987e5972SCameron Grant bus_space_tag_t st; 66987e5972SCameron Grant bus_space_handle_t sh; 67987e5972SCameron Grant bus_dma_tag_t parent_dmat; 68987e5972SCameron Grant 69987e5972SCameron Grant struct resource *reg, *irq; 70987e5972SCameron Grant int regtype, regid, irqid; 71987e5972SCameron Grant void *ih; 72987e5972SCameron Grant 73987e5972SCameron Grant u_int32_t playchns; 74987e5972SCameron Grant struct tr_chinfo chinfo[TR_MAXPLAYCH]; 75987e5972SCameron Grant struct tr_chinfo recchinfo; 76987e5972SCameron Grant }; 77987e5972SCameron Grant 78987e5972SCameron Grant /* -------------------------------------------------------------------- */ 79987e5972SCameron Grant 80987e5972SCameron Grant /* 81987e5972SCameron Grant * prototypes 82987e5972SCameron Grant */ 83987e5972SCameron Grant 84987e5972SCameron Grant /* channel interface */ 85987e5972SCameron Grant static void *trchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 86987e5972SCameron Grant static int trchan_setdir(void *data, int dir); 87987e5972SCameron Grant static int trchan_setformat(void *data, u_int32_t format); 88987e5972SCameron Grant static int trchan_setspeed(void *data, u_int32_t speed); 89987e5972SCameron Grant static int trchan_setblocksize(void *data, u_int32_t blocksize); 90987e5972SCameron Grant static int trchan_trigger(void *data, int go); 91987e5972SCameron Grant static int trchan_getptr(void *data); 92987e5972SCameron Grant static pcmchan_caps *trchan_getcaps(void *data); 93987e5972SCameron Grant 94987e5972SCameron Grant /* talk to the codec - called from ac97.c */ 95987e5972SCameron Grant static u_int32_t tr_rdcd(void *, int); 96987e5972SCameron Grant static void tr_wrcd(void *, int, u_int32_t); 97987e5972SCameron Grant 98987e5972SCameron Grant /* stuff */ 99987e5972SCameron Grant static int tr_init(struct tr_info *); 100987e5972SCameron Grant static void tr_intr(void *); 101987e5972SCameron Grant 102987e5972SCameron Grant /* talk to the card */ 103987e5972SCameron Grant static u_int32_t tr_rd(struct tr_info *, int, int); 104987e5972SCameron Grant static void tr_wr(struct tr_info *, int, u_int32_t, int); 105987e5972SCameron Grant 106987e5972SCameron Grant /* manipulate playback channels */ 107987e5972SCameron Grant static void tr_clrint(struct tr_info *, char); 108987e5972SCameron Grant static void tr_enaint(struct tr_info *, char, int); 109987e5972SCameron Grant static u_int32_t tr_testint(struct tr_info *, char); 110987e5972SCameron Grant static void tr_rdch(struct tr_info *, char, struct tr_chinfo *); 111987e5972SCameron Grant static void tr_wrch(struct tr_info *, char, struct tr_chinfo *); 112987e5972SCameron Grant static void tr_selch(struct tr_info *, char); 113987e5972SCameron Grant static void tr_startch(struct tr_info *, char); 114987e5972SCameron Grant static void tr_stopch(struct tr_info *, char); 115987e5972SCameron Grant 116987e5972SCameron Grant /* -------------------------------------------------------------------- */ 117987e5972SCameron Grant 118987e5972SCameron Grant static pcmchan_caps tr_reccaps = { 119987e5972SCameron Grant 4000, 48000, 120987e5972SCameron Grant AFMT_STEREO | AFMT_U8 | AFMT_S8 | AFMT_S16_LE | AFMT_U16_LE, 121987e5972SCameron Grant AFMT_STEREO | AFMT_S16_LE 122987e5972SCameron Grant }; 123987e5972SCameron Grant 124987e5972SCameron Grant static pcmchan_caps tr_playcaps = { 125987e5972SCameron Grant 4000, 48000, 126987e5972SCameron Grant AFMT_STEREO | AFMT_U8 | AFMT_S8 | AFMT_S16_LE | AFMT_U16_LE, 1270a12fe07SCameron Grant AFMT_STEREO | AFMT_S16_LE 128987e5972SCameron Grant }; 129987e5972SCameron Grant 130987e5972SCameron Grant static pcm_channel tr_chantemplate = { 131987e5972SCameron Grant trchan_init, 132987e5972SCameron Grant trchan_setdir, 133987e5972SCameron Grant trchan_setformat, 134987e5972SCameron Grant trchan_setspeed, 135987e5972SCameron Grant trchan_setblocksize, 136987e5972SCameron Grant trchan_trigger, 137987e5972SCameron Grant trchan_getptr, 138987e5972SCameron Grant trchan_getcaps, 139987e5972SCameron Grant }; 140987e5972SCameron Grant 141987e5972SCameron Grant /* -------------------------------------------------------------------- */ 142987e5972SCameron Grant 143987e5972SCameron Grant static u_int32_t 144987e5972SCameron Grant tr_fmttobits(u_int32_t fmt) 145987e5972SCameron Grant { 146987e5972SCameron Grant u_int32_t bits = 0; 147987e5972SCameron Grant bits |= (fmt & AFMT_STEREO)? 0x4 : 0; 148987e5972SCameron Grant bits |= (fmt & (AFMT_S8 | AFMT_S16_LE))? 0x2 : 0; 149987e5972SCameron Grant bits |= (fmt & (AFMT_S16_LE | AFMT_U16_LE))? 0x8 : 0; 150987e5972SCameron Grant return bits; 151987e5972SCameron Grant } 152987e5972SCameron Grant 153987e5972SCameron Grant /* Hardware */ 154987e5972SCameron Grant 155987e5972SCameron Grant static u_int32_t 156987e5972SCameron Grant tr_rd(struct tr_info *tr, int regno, int size) 157987e5972SCameron Grant { 158987e5972SCameron Grant switch(size) { 159987e5972SCameron Grant case 1: 160987e5972SCameron Grant return bus_space_read_1(tr->st, tr->sh, regno); 161987e5972SCameron Grant case 2: 162987e5972SCameron Grant return bus_space_read_2(tr->st, tr->sh, regno); 163987e5972SCameron Grant case 4: 164987e5972SCameron Grant return bus_space_read_4(tr->st, tr->sh, regno); 165987e5972SCameron Grant default: 166987e5972SCameron Grant return 0xffffffff; 167987e5972SCameron Grant } 168987e5972SCameron Grant } 169987e5972SCameron Grant 170987e5972SCameron Grant static void 171987e5972SCameron Grant tr_wr(struct tr_info *tr, int regno, u_int32_t data, int size) 172987e5972SCameron Grant { 173987e5972SCameron Grant switch(size) { 174987e5972SCameron Grant case 1: 175987e5972SCameron Grant bus_space_write_1(tr->st, tr->sh, regno, data); 176987e5972SCameron Grant break; 177987e5972SCameron Grant case 2: 178987e5972SCameron Grant bus_space_write_2(tr->st, tr->sh, regno, data); 179987e5972SCameron Grant break; 180987e5972SCameron Grant case 4: 181987e5972SCameron Grant bus_space_write_4(tr->st, tr->sh, regno, data); 182987e5972SCameron Grant break; 183987e5972SCameron Grant } 184987e5972SCameron Grant } 185987e5972SCameron Grant 186987e5972SCameron Grant /* ac97 codec */ 187987e5972SCameron Grant 188987e5972SCameron Grant static u_int32_t 189987e5972SCameron Grant tr_rdcd(void *devinfo, int regno) 190987e5972SCameron Grant { 191987e5972SCameron Grant struct tr_info *tr = (struct tr_info *)devinfo; 192987e5972SCameron Grant int i, j, treg, trw; 193987e5972SCameron Grant 194987e5972SCameron Grant switch (tr->type) { 195987e5972SCameron Grant case TDX_PCI_ID: 196987e5972SCameron Grant treg=TDX_REG_CODECRD; 197987e5972SCameron Grant trw=TDX_CDC_RWSTAT; 198987e5972SCameron Grant break; 199987e5972SCameron Grant case TNX_PCI_ID: 200987e5972SCameron Grant treg=(regno & 0x100)? TNX_REG_CODEC2RD : TNX_REG_CODEC1RD; 201987e5972SCameron Grant trw=TNX_CDC_RWSTAT; 202987e5972SCameron Grant break; 203987e5972SCameron Grant default: 204987e5972SCameron Grant printf("!!! tr_rdcd defaulted !!!\n"); 205987e5972SCameron Grant return 0xffffffff; 206987e5972SCameron Grant } 207987e5972SCameron Grant 208987e5972SCameron Grant regno &= 0x7f; 209987e5972SCameron Grant tr_wr(tr, treg, regno | trw, 4); 210987e5972SCameron Grant j=trw; 211987e5972SCameron Grant for (i=TR_TIMEOUT_CDC; (i > 0) && (j & trw); i--) j=tr_rd(tr, treg, 4); 212987e5972SCameron Grant if (i == 0) printf("codec timeout during read of register %x\n", regno); 213987e5972SCameron Grant return (j >> TR_CDC_DATA) & 0xffff; 214987e5972SCameron Grant } 215987e5972SCameron Grant 216987e5972SCameron Grant static void 217987e5972SCameron Grant tr_wrcd(void *devinfo, int regno, u_int32_t data) 218987e5972SCameron Grant { 219987e5972SCameron Grant struct tr_info *tr = (struct tr_info *)devinfo; 220987e5972SCameron Grant int i, j, treg, trw; 221987e5972SCameron Grant 222987e5972SCameron Grant switch (tr->type) { 223987e5972SCameron Grant case TDX_PCI_ID: 224987e5972SCameron Grant treg=TDX_REG_CODECWR; 225987e5972SCameron Grant trw=TDX_CDC_RWSTAT; 226987e5972SCameron Grant break; 227987e5972SCameron Grant case TNX_PCI_ID: 228987e5972SCameron Grant treg=TNX_REG_CODECWR; 229987e5972SCameron Grant trw=TNX_CDC_RWSTAT | ((regno & 0x100)? TNX_CDC_SEC : 0); 230987e5972SCameron Grant break; 231987e5972SCameron Grant default: 232987e5972SCameron Grant printf("!!! tr_wrcd defaulted !!!"); 233987e5972SCameron Grant return; 234987e5972SCameron Grant } 235987e5972SCameron Grant 236987e5972SCameron Grant regno &= 0x7f; 237987e5972SCameron Grant #if 0 238987e5972SCameron Grant printf("tr_wrcd: reg %x was %x", regno, tr_rdcd(devinfo, regno)); 239987e5972SCameron Grant #endif 240987e5972SCameron Grant j=trw; 241987e5972SCameron Grant for (i=TR_TIMEOUT_CDC; (i>0) && (j & trw); i--) j=tr_rd(tr, treg, 4); 242987e5972SCameron Grant tr_wr(tr, treg, (data << TR_CDC_DATA) | regno | trw, 4); 243987e5972SCameron Grant #if 0 244987e5972SCameron Grant printf(" - wrote %x, now %x\n", data, tr_rdcd(devinfo, regno)); 245987e5972SCameron Grant #endif 246987e5972SCameron Grant if (i==0) printf("codec timeout writing %x, data %x\n", regno, data); 247987e5972SCameron Grant } 248987e5972SCameron Grant 249987e5972SCameron Grant /* playback channel interrupts */ 250987e5972SCameron Grant 251987e5972SCameron Grant static u_int32_t 252987e5972SCameron Grant tr_testint(struct tr_info *tr, char channel) 253987e5972SCameron Grant { 254987e5972SCameron Grant return tr_rd(tr, (channel & 0x20)? TR_REG_ADDRINTB : TR_REG_ADDRINTA, 255987e5972SCameron Grant 4) & (1<<(channel & 0x1f)); 256987e5972SCameron Grant } 257987e5972SCameron Grant 258987e5972SCameron Grant static void 259987e5972SCameron Grant tr_clrint(struct tr_info *tr, char channel) 260987e5972SCameron Grant { 261987e5972SCameron Grant tr_wr(tr, (channel & 0x20)? TR_REG_ADDRINTB : TR_REG_ADDRINTA, 262987e5972SCameron Grant 1<<(channel & 0x1f), 4); 263987e5972SCameron Grant } 264987e5972SCameron Grant 265987e5972SCameron Grant static void 266987e5972SCameron Grant tr_enaint(struct tr_info *tr, char channel, int enable) 267987e5972SCameron Grant { 268987e5972SCameron Grant u_int32_t reg = (channel & 0x20)? TR_REG_INTENB : TR_REG_INTENA; 269987e5972SCameron Grant u_int32_t i = tr_rd(tr, reg, 4); 270987e5972SCameron Grant channel &= 0x1f; 271987e5972SCameron Grant i &= ~(1 << channel); 272987e5972SCameron Grant i |= (enable? 1 : 0) << channel; 273987e5972SCameron Grant tr_clrint(tr, channel); 274987e5972SCameron Grant tr_wr(tr, reg, i, 4); 275987e5972SCameron Grant } 276987e5972SCameron Grant 277987e5972SCameron Grant /* playback channels */ 278987e5972SCameron Grant 279987e5972SCameron Grant static void 280987e5972SCameron Grant tr_selch(struct tr_info *tr, char channel) 281987e5972SCameron Grant { 282987e5972SCameron Grant int i=tr_rd(tr, TR_REG_CIR, 4); 283987e5972SCameron Grant i &= ~TR_CIR_MASK; 284987e5972SCameron Grant i |= channel & 0x3f; 285987e5972SCameron Grant tr_wr(tr, TR_REG_CIR, i, 4); 286987e5972SCameron Grant } 287987e5972SCameron Grant 288987e5972SCameron Grant static void 289987e5972SCameron Grant tr_startch(struct tr_info *tr, char channel) 290987e5972SCameron Grant { 291987e5972SCameron Grant tr_wr(tr, (channel & 0x20)? TR_REG_STARTB : TR_REG_STARTA, 292987e5972SCameron Grant 1<<(channel & 0x1f), 4); 293987e5972SCameron Grant } 294987e5972SCameron Grant 295987e5972SCameron Grant static void 296987e5972SCameron Grant tr_stopch(struct tr_info *tr, char channel) 297987e5972SCameron Grant { 298987e5972SCameron Grant tr_wr(tr, (channel & 0x20)? TR_REG_STOPB : TR_REG_STOPA, 299987e5972SCameron Grant 1<<(channel & 0x1f), 4); 300987e5972SCameron Grant } 301987e5972SCameron Grant 302987e5972SCameron Grant static void 303987e5972SCameron Grant tr_wrch(struct tr_info *tr, char channel, struct tr_chinfo *ch) 304987e5972SCameron Grant { 305987e5972SCameron Grant u_int32_t cr[TR_CHN_REGS], i; 306987e5972SCameron Grant 307987e5972SCameron Grant ch->gvsel &= 0x00000001; 308987e5972SCameron Grant ch->fmc &= 0x00000003; 309987e5972SCameron Grant ch->fms &= 0x0000000f; 310987e5972SCameron Grant ch->ctrl &= 0x0000000f; 311987e5972SCameron Grant ch->pan &= 0x0000007f; 312987e5972SCameron Grant ch->rvol &= 0x0000007f; 313987e5972SCameron Grant ch->cvol &= 0x0000007f; 314987e5972SCameron Grant ch->vol &= 0x000000ff; 315987e5972SCameron Grant ch->ec &= 0x00000fff; 316987e5972SCameron Grant ch->alpha &= 0x00000fff; 317987e5972SCameron Grant ch->delta &= 0x0000ffff; 318987e5972SCameron Grant ch->lba &= 0x3fffffff; 319987e5972SCameron Grant 320987e5972SCameron Grant cr[1]=ch->lba; 321987e5972SCameron Grant cr[3]=(ch->rvol<<7) | (ch->cvol); 322987e5972SCameron Grant cr[4]=(ch->gvsel<<31)|(ch->pan<<24)|(ch->vol<<16)|(ch->ctrl<<12)|(ch->ec); 323987e5972SCameron Grant 324987e5972SCameron Grant switch (tr->type) { 325987e5972SCameron Grant case TDX_PCI_ID: 326987e5972SCameron Grant ch->cso &= 0x0000ffff; 327987e5972SCameron Grant ch->eso &= 0x0000ffff; 328987e5972SCameron Grant cr[0]=(ch->cso<<16) | (ch->alpha<<4) | (ch->fms); 329987e5972SCameron Grant cr[2]=(ch->eso<<16) | (ch->delta); 330987e5972SCameron Grant cr[3]|=0x0000c000; 331987e5972SCameron Grant break; 332987e5972SCameron Grant case TNX_PCI_ID: 333987e5972SCameron Grant ch->cso &= 0x00ffffff; 334987e5972SCameron Grant ch->eso &= 0x00ffffff; 335987e5972SCameron Grant cr[0]=((ch->delta & 0xff)<<24) | (ch->cso); 336987e5972SCameron Grant cr[2]=((ch->delta>>16)<<24) | (ch->eso); 337987e5972SCameron Grant cr[3]|=(ch->alpha<<20) | (ch->fms<<16) | (ch->fmc<<14); 338987e5972SCameron Grant break; 339987e5972SCameron Grant } 340987e5972SCameron Grant tr_selch(tr, channel); 341987e5972SCameron Grant for (i=0; i<TR_CHN_REGS; i++) 342987e5972SCameron Grant tr_wr(tr, TR_REG_CHNBASE+(i<<2), cr[i], 4); 343987e5972SCameron Grant } 344987e5972SCameron Grant 345987e5972SCameron Grant static void 346987e5972SCameron Grant tr_rdch(struct tr_info *tr, char channel, struct tr_chinfo *ch) 347987e5972SCameron Grant { 348987e5972SCameron Grant u_int32_t cr[5], i; 349987e5972SCameron Grant tr_selch(tr, channel); 350987e5972SCameron Grant for (i=0; i<5; i++) cr[i]=tr_rd(tr, TR_REG_CHNBASE+(i<<2), 4); 351987e5972SCameron Grant ch->lba= (cr[1] & 0x3fffffff); 352987e5972SCameron Grant ch->fmc= (cr[3] & 0x0000c000) >> 14; 353987e5972SCameron Grant ch->rvol= (cr[3] & 0x00003f80) >> 7; 354987e5972SCameron Grant ch->cvol= (cr[3] & 0x0000007f); 355987e5972SCameron Grant ch->gvsel= (cr[4] & 0x80000000) >> 31; 356987e5972SCameron Grant ch->pan= (cr[4] & 0x7f000000) >> 24; 357987e5972SCameron Grant ch->vol= (cr[4] & 0x00ff0000) >> 16; 358987e5972SCameron Grant ch->ctrl= (cr[4] & 0x0000f000) >> 12; 359987e5972SCameron Grant ch->ec= (cr[4] & 0x00000fff); 360987e5972SCameron Grant switch(tr->type) { 361987e5972SCameron Grant case TDX_PCI_ID: 362987e5972SCameron Grant ch->cso= (cr[0] & 0xffff0000) >> 16; 363987e5972SCameron Grant ch->alpha= (cr[0] & 0x0000fff0) >> 4; 364987e5972SCameron Grant ch->fms= (cr[0] & 0x0000000f); 365987e5972SCameron Grant ch->eso= (cr[2] & 0xffff0000) >> 16; 366987e5972SCameron Grant ch->delta= (cr[2] & 0x0000ffff); 367987e5972SCameron Grant break; 368987e5972SCameron Grant case TNX_PCI_ID: 369987e5972SCameron Grant ch->cso= (cr[0] & 0x00ffffff); 370987e5972SCameron Grant ch->eso= (cr[2] & 0x00ffffff); 371987e5972SCameron Grant ch->delta= ((cr[2] & 0xff000000) >> 16) | 372987e5972SCameron Grant ((cr[0] & 0xff000000) >> 24); 373987e5972SCameron Grant ch->alpha= (cr[3] & 0xfff00000) >> 20; 374987e5972SCameron Grant ch->fms= (cr[3] & 0x000f0000) >> 16; 375987e5972SCameron Grant break; 376987e5972SCameron Grant } 377987e5972SCameron Grant } 378987e5972SCameron Grant 379987e5972SCameron Grant /* channel interface */ 380987e5972SCameron Grant 381987e5972SCameron Grant void * 382987e5972SCameron Grant trchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 383987e5972SCameron Grant { 384987e5972SCameron Grant struct tr_info *tr = devinfo; 385987e5972SCameron Grant struct tr_chinfo *ch; 386987e5972SCameron Grant if (dir == PCMDIR_PLAY) { 387987e5972SCameron Grant ch = &tr->chinfo[tr->playchns]; 388987e5972SCameron Grant ch->index = tr->playchns++; 389987e5972SCameron Grant } else { 390987e5972SCameron Grant ch = &tr->recchinfo; 391987e5972SCameron Grant ch->index = -1; 392987e5972SCameron Grant } 393987e5972SCameron Grant ch->buffer = b; 394987e5972SCameron Grant ch->buffer->bufsize = TR_BUFFSIZE; 395987e5972SCameron Grant ch->parent = tr; 396987e5972SCameron Grant ch->channel = c; 397987e5972SCameron Grant if (chn_allocbuf(ch->buffer, tr->parent_dmat) == -1) return NULL; 398987e5972SCameron Grant else return ch; 399987e5972SCameron Grant } 400987e5972SCameron Grant 401987e5972SCameron Grant static int 402987e5972SCameron Grant trchan_setdir(void *data, int dir) 403987e5972SCameron Grant { 404987e5972SCameron Grant struct tr_chinfo *ch = data; 405987e5972SCameron Grant struct tr_info *tr = ch->parent; 406987e5972SCameron Grant if (dir == PCMDIR_PLAY && ch->index >= 0) { 407987e5972SCameron Grant ch->fmc = ch->fms = ch->ec = ch->alpha = 0; 408987e5972SCameron Grant ch->lba = vtophys(ch->buffer->buf); 409987e5972SCameron Grant ch->cso = 0; 410987e5972SCameron Grant ch->eso = ch->buffer->bufsize - 1; 411987e5972SCameron Grant ch->rvol = ch->cvol = 0; 412987e5972SCameron Grant ch->gvsel = 0; 413987e5972SCameron Grant ch->pan = 0; 414987e5972SCameron Grant ch->vol = 0; 415987e5972SCameron Grant ch->ctrl = 0x01; 416987e5972SCameron Grant ch->delta = 0; 417987e5972SCameron Grant tr_wrch(tr, ch->index, ch); 418987e5972SCameron Grant tr_enaint(tr, ch->index, 1); 419987e5972SCameron Grant } else if (dir == PCMDIR_REC && ch->index == -1) { 420987e5972SCameron Grant /* set up dma mode regs */ 421987e5972SCameron Grant u_int32_t i; 422987e5972SCameron Grant tr_wr(tr, TR_REG_DMAR15, 0, 1); 423987e5972SCameron Grant i = tr_rd(tr, TR_REG_DMAR11, 1) & 0x03; 424987e5972SCameron Grant tr_wr(tr, TR_REG_DMAR11, i | 0x54, 1); 425987e5972SCameron Grant /* set up base address */ 426987e5972SCameron Grant tr_wr(tr, TR_REG_DMAR0, vtophys(ch->buffer->buf), 4); 427987e5972SCameron Grant /* set up buffer size */ 428987e5972SCameron Grant i = tr_rd(tr, TR_REG_DMAR4, 4) & ~0x00ffffff; 429987e5972SCameron Grant tr_wr(tr, TR_REG_DMAR4, i | (ch->buffer->bufsize - 1), 4); 430987e5972SCameron Grant } else return -1; 431987e5972SCameron Grant return 0; 432987e5972SCameron Grant } 433987e5972SCameron Grant 434987e5972SCameron Grant static int 435987e5972SCameron Grant trchan_setformat(void *data, u_int32_t format) 436987e5972SCameron Grant { 437987e5972SCameron Grant struct tr_chinfo *ch = data; 438987e5972SCameron Grant struct tr_info *tr = ch->parent; 439987e5972SCameron Grant u_int32_t bits = tr_fmttobits(format); 440987e5972SCameron Grant 441987e5972SCameron Grant if (ch->index >= 0) { 442987e5972SCameron Grant tr_rdch(tr, ch->index, ch); 443987e5972SCameron Grant ch->eso = (ch->buffer->bufsize / ch->buffer->sample_size) - 1; 444987e5972SCameron Grant ch->ctrl = bits | 0x01; 445987e5972SCameron Grant tr_wrch(tr, ch->index, ch); 446987e5972SCameron Grant } else { 447987e5972SCameron Grant u_int32_t i; 448987e5972SCameron Grant /* set # of samples between interrupts */ 449987e5972SCameron Grant i = (TR_INTSAMPLES >> ((bits & 0x08)? 1 : 0)) - 1; 450987e5972SCameron Grant tr_wr(tr, TR_REG_SBBL, i | (i << 16), 4); 451987e5972SCameron Grant /* set sample format */ 452987e5972SCameron Grant i = 0x18 | (bits << 4); 453987e5972SCameron Grant tr_wr(tr, TR_REG_SBCTRL, i, 1); 454987e5972SCameron Grant } 455987e5972SCameron Grant return 0; 456987e5972SCameron Grant } 457987e5972SCameron Grant 458987e5972SCameron Grant static int 459987e5972SCameron Grant trchan_setspeed(void *data, u_int32_t speed) 460987e5972SCameron Grant { 461987e5972SCameron Grant struct tr_chinfo *ch = data; 462987e5972SCameron Grant struct tr_info *tr = ch->parent; 463987e5972SCameron Grant 464987e5972SCameron Grant if (ch->index >= 0) { 465987e5972SCameron Grant tr_rdch(tr, ch->index, ch); 466987e5972SCameron Grant ch->delta = (speed << 12) / 48000; 467987e5972SCameron Grant tr_wrch(tr, ch->index, ch); 468987e5972SCameron Grant return (ch->delta * 48000) >> 12; 469987e5972SCameron Grant } else { 470987e5972SCameron Grant /* setup speed */ 471987e5972SCameron Grant ch->delta = (48000 << 12) / speed; 472987e5972SCameron Grant tr_wr(tr, TR_REG_SBDELTA, ch->delta, 2); 473987e5972SCameron Grant return (48000 << 12) / ch->delta; 474987e5972SCameron Grant } 475987e5972SCameron Grant return 0; 476987e5972SCameron Grant } 477987e5972SCameron Grant 478987e5972SCameron Grant static int 479987e5972SCameron Grant trchan_setblocksize(void *data, u_int32_t blocksize) 480987e5972SCameron Grant { 481987e5972SCameron Grant struct tr_chinfo *ch = data; 482987e5972SCameron Grant return ch->buffer->bufsize / 2; 483987e5972SCameron Grant } 484987e5972SCameron Grant 485987e5972SCameron Grant static int 486987e5972SCameron Grant trchan_trigger(void *data, int go) 487987e5972SCameron Grant { 488987e5972SCameron Grant struct tr_chinfo *ch = data; 489987e5972SCameron Grant struct tr_info *tr = ch->parent; 490674c45bdSCameron Grant 491a0b57fb7SCameron Grant if (go == PCMTRIG_EMLDMAWR) return 0; 492987e5972SCameron Grant if (ch->index >= 0) { 493674c45bdSCameron Grant if (go == PCMTRIG_START) { 494674c45bdSCameron Grant tr_rdch(tr, ch->index, ch); 495674c45bdSCameron Grant ch->cso = 0; 496674c45bdSCameron Grant tr_wrch(tr, ch->index, ch); 497674c45bdSCameron Grant tr_startch(tr, ch->index); 498674c45bdSCameron Grant } else tr_stopch(tr, ch->index); 499987e5972SCameron Grant } else { 500987e5972SCameron Grant u_int32_t i = tr_rd(tr, TR_REG_SBCTRL, 1) & ~7; 501987e5972SCameron Grant tr_wr(tr, TR_REG_SBCTRL, i | (go == PCMTRIG_START)? 1 : 0, 1); 502987e5972SCameron Grant } 503987e5972SCameron Grant return 0; 504987e5972SCameron Grant } 505987e5972SCameron Grant 506987e5972SCameron Grant static int 507987e5972SCameron Grant trchan_getptr(void *data) 508987e5972SCameron Grant { 509987e5972SCameron Grant struct tr_chinfo *ch = data; 510987e5972SCameron Grant struct tr_info *tr = ch->parent; 511987e5972SCameron Grant if (ch->index >= 0) { 512987e5972SCameron Grant tr_rdch(tr, ch->index, ch); 513987e5972SCameron Grant return ch->cso * ch->buffer->sample_size; 514987e5972SCameron Grant } else return tr_rd(tr, TR_REG_DMAR0, 4) - vtophys(ch->buffer->buf); 515987e5972SCameron Grant } 516987e5972SCameron Grant 517987e5972SCameron Grant static pcmchan_caps * 518987e5972SCameron Grant trchan_getcaps(void *data) 519987e5972SCameron Grant { 520987e5972SCameron Grant struct tr_chinfo *ch = data; 521987e5972SCameron Grant return (ch->index >= 0)? &tr_playcaps : &tr_reccaps; 522987e5972SCameron Grant } 523987e5972SCameron Grant 524987e5972SCameron Grant /* The interrupt handler */ 525987e5972SCameron Grant 526987e5972SCameron Grant static void 527987e5972SCameron Grant tr_intr(void *p) 528987e5972SCameron Grant { 529987e5972SCameron Grant struct tr_info *tr = (struct tr_info *)p; 530987e5972SCameron Grant u_int32_t intsrc = tr_rd(tr, TR_REG_MISCINT, 4); 531987e5972SCameron Grant 532987e5972SCameron Grant if (intsrc & TR_INT_ADDR) { 533987e5972SCameron Grant int i; 534987e5972SCameron Grant for (i = 0; i < tr->playchns; i++) { 535987e5972SCameron Grant if (tr_testint(tr, i)) { 536987e5972SCameron Grant chn_intr(tr->chinfo[i].channel); 537987e5972SCameron Grant tr_clrint(tr, i); 538987e5972SCameron Grant } 539987e5972SCameron Grant } 540987e5972SCameron Grant } 541987e5972SCameron Grant if (intsrc & TR_INT_SB) { 542987e5972SCameron Grant chn_intr(tr->recchinfo.channel); 543987e5972SCameron Grant tr_rd(tr, TR_REG_SBR9, 1); 544987e5972SCameron Grant tr_rd(tr, TR_REG_SBR10, 1); 545987e5972SCameron Grant } 546987e5972SCameron Grant } 547987e5972SCameron Grant 548987e5972SCameron Grant /* -------------------------------------------------------------------- */ 549987e5972SCameron Grant 550987e5972SCameron Grant /* 551987e5972SCameron Grant * Probe and attach the card 552987e5972SCameron Grant */ 553987e5972SCameron Grant 554987e5972SCameron Grant static int 555987e5972SCameron Grant tr_init(struct tr_info *tr) 556987e5972SCameron Grant { 557987e5972SCameron Grant if (tr->type == TDX_PCI_ID) { 558987e5972SCameron Grant tr_wr(tr, TDX_REG_CODECST, TDX_CDC_ON, 4); 559987e5972SCameron Grant } else tr_wr(tr, TNX_REG_CODECST, TNX_CDC_ON, 4); 560987e5972SCameron Grant 561987e5972SCameron Grant tr_wr(tr, TR_REG_CIR, TR_CIR_MIDENA | TR_CIR_ADDRENA, 4); 562987e5972SCameron Grant tr->playchns = 0; 563987e5972SCameron Grant return 0; 564987e5972SCameron Grant } 565987e5972SCameron Grant 566987e5972SCameron Grant static int 567987e5972SCameron Grant tr_pci_probe(device_t dev) 568987e5972SCameron Grant { 569987e5972SCameron Grant if (pci_get_devid(dev) == TDX_PCI_ID) { 570987e5972SCameron Grant device_set_desc(dev, "Trident 4DWave DX"); 571987e5972SCameron Grant return 0; 572987e5972SCameron Grant } 573987e5972SCameron Grant if (pci_get_devid(dev) == TNX_PCI_ID) { 574987e5972SCameron Grant device_set_desc(dev, "Trident 4DWave NX"); 575987e5972SCameron Grant return 0; 576987e5972SCameron Grant } 577987e5972SCameron Grant 578987e5972SCameron Grant return ENXIO; 579987e5972SCameron Grant } 580987e5972SCameron Grant 581987e5972SCameron Grant static int 582987e5972SCameron Grant tr_pci_attach(device_t dev) 583987e5972SCameron Grant { 584987e5972SCameron Grant snddev_info *d; 585987e5972SCameron Grant u_int32_t data; 586987e5972SCameron Grant struct tr_info *tr; 587987e5972SCameron Grant struct ac97_info *codec; 588987e5972SCameron Grant int i; 589987e5972SCameron Grant int mapped; 590987e5972SCameron Grant char status[SND_STATUSLEN]; 591987e5972SCameron Grant 592987e5972SCameron Grant d = device_get_softc(dev); 593987e5972SCameron Grant if ((tr = malloc(sizeof(*tr), M_DEVBUF, M_NOWAIT)) == NULL) { 594987e5972SCameron Grant device_printf(dev, "cannot allocate softc\n"); 595987e5972SCameron Grant return ENXIO; 596987e5972SCameron Grant } 597987e5972SCameron Grant 598987e5972SCameron Grant bzero(tr, sizeof(*tr)); 599987e5972SCameron Grant tr->type = pci_get_devid(dev); 600987e5972SCameron Grant 601987e5972SCameron Grant data = pci_read_config(dev, PCIR_COMMAND, 2); 602987e5972SCameron Grant data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 603987e5972SCameron Grant pci_write_config(dev, PCIR_COMMAND, data, 2); 604987e5972SCameron Grant data = pci_read_config(dev, PCIR_COMMAND, 2); 605987e5972SCameron Grant 606987e5972SCameron Grant mapped = 0; 607987e5972SCameron Grant /* XXX dfr: is this strictly necessary? */ 608987e5972SCameron Grant for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) { 609987e5972SCameron Grant tr->regid = PCIR_MAPS + i*4; 610987e5972SCameron Grant tr->regtype = SYS_RES_MEMORY; 611987e5972SCameron Grant tr->reg = bus_alloc_resource(dev, tr->regtype, &tr->regid, 612987e5972SCameron Grant 0, ~0, 1, RF_ACTIVE); 613987e5972SCameron Grant if (!tr->reg) { 614987e5972SCameron Grant tr->regtype = SYS_RES_IOPORT; 615987e5972SCameron Grant tr->reg = bus_alloc_resource(dev, tr->regtype, 616987e5972SCameron Grant &tr->regid, 0, ~0, 1, 617987e5972SCameron Grant RF_ACTIVE); 618987e5972SCameron Grant } 619987e5972SCameron Grant if (tr->reg) { 620987e5972SCameron Grant tr->st = rman_get_bustag(tr->reg); 621987e5972SCameron Grant tr->sh = rman_get_bushandle(tr->reg); 622987e5972SCameron Grant mapped++; 623987e5972SCameron Grant } 624987e5972SCameron Grant } 625987e5972SCameron Grant 626987e5972SCameron Grant if (mapped == 0) { 627987e5972SCameron Grant device_printf(dev, "unable to map register space\n"); 628987e5972SCameron Grant goto bad; 629987e5972SCameron Grant } 630987e5972SCameron Grant 631987e5972SCameron Grant if (tr_init(tr) == -1) { 632987e5972SCameron Grant device_printf(dev, "unable to initialize the card\n"); 633987e5972SCameron Grant goto bad; 634987e5972SCameron Grant } 635987e5972SCameron Grant 63639004e69SCameron Grant codec = ac97_create(dev, tr, NULL, tr_rdcd, tr_wrcd); 637987e5972SCameron Grant if (codec == NULL) goto bad; 638e620d959SCameron Grant if (mixer_init(d, &ac97_mixer, codec) == -1) goto bad; 639987e5972SCameron Grant 640987e5972SCameron Grant tr->irqid = 0; 641987e5972SCameron Grant tr->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &tr->irqid, 642987e5972SCameron Grant 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 643987e5972SCameron Grant if (!tr->irq || 644987e5972SCameron Grant bus_setup_intr(dev, tr->irq, INTR_TYPE_TTY, tr_intr, tr, &tr->ih)) { 645987e5972SCameron Grant device_printf(dev, "unable to map interrupt\n"); 646987e5972SCameron Grant goto bad; 647987e5972SCameron Grant } 648987e5972SCameron Grant 649987e5972SCameron Grant if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 650987e5972SCameron Grant /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 651987e5972SCameron Grant /*highaddr*/BUS_SPACE_MAXADDR, 652987e5972SCameron Grant /*filter*/NULL, /*filterarg*/NULL, 653987e5972SCameron Grant /*maxsize*/TR_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff, 654987e5972SCameron Grant /*flags*/0, &tr->parent_dmat) != 0) { 655987e5972SCameron Grant device_printf(dev, "unable to create dma tag\n"); 656987e5972SCameron Grant goto bad; 657987e5972SCameron Grant } 658987e5972SCameron Grant 659987e5972SCameron Grant snprintf(status, 64, "at %s 0x%lx irq %ld", 660987e5972SCameron Grant (tr->regtype == SYS_RES_IOPORT)? "io" : "memory", 661987e5972SCameron Grant rman_get_start(tr->reg), rman_get_start(tr->irq)); 662987e5972SCameron Grant 663987e5972SCameron Grant if (pcm_register(dev, tr, TR_MAXPLAYCH, 1)) goto bad; 664987e5972SCameron Grant pcm_addchan(dev, PCMDIR_REC, &tr_chantemplate, tr); 665987e5972SCameron Grant for (i = 0; i < TR_MAXPLAYCH; i++) 666987e5972SCameron Grant pcm_addchan(dev, PCMDIR_PLAY, &tr_chantemplate, tr); 667987e5972SCameron Grant pcm_setstatus(dev, status); 668987e5972SCameron Grant 669987e5972SCameron Grant return 0; 670987e5972SCameron Grant 671987e5972SCameron Grant bad: 672987e5972SCameron Grant if (tr->reg) bus_release_resource(dev, tr->regtype, tr->regid, tr->reg); 673987e5972SCameron Grant if (tr->ih) bus_teardown_intr(dev, tr->irq, tr->ih); 674987e5972SCameron Grant if (tr->irq) bus_release_resource(dev, SYS_RES_IRQ, tr->irqid, tr->irq); 675987e5972SCameron Grant free(tr, M_DEVBUF); 676987e5972SCameron Grant return ENXIO; 677987e5972SCameron Grant } 678987e5972SCameron Grant 679987e5972SCameron Grant static device_method_t tr_methods[] = { 680987e5972SCameron Grant /* Device interface */ 681987e5972SCameron Grant DEVMETHOD(device_probe, tr_pci_probe), 682987e5972SCameron Grant DEVMETHOD(device_attach, tr_pci_attach), 683987e5972SCameron Grant 684987e5972SCameron Grant { 0, 0 } 685987e5972SCameron Grant }; 686987e5972SCameron Grant 687987e5972SCameron Grant static driver_t tr_driver = { 688987e5972SCameron Grant "pcm", 689987e5972SCameron Grant tr_methods, 690987e5972SCameron Grant sizeof(snddev_info), 691987e5972SCameron Grant }; 692987e5972SCameron Grant 693987e5972SCameron Grant static devclass_t pcm_devclass; 694987e5972SCameron Grant 695987e5972SCameron Grant DRIVER_MODULE(tr, pci, tr_driver, pcm_devclass, 0, 0); 696