1 /* 2 * Support the ENSONIQ AudioPCI board and Creative Labs SoundBlaster PCI 3 * boards based on the ES1370, ES1371 and ES1373 chips. 4 * 5 * Copyright (c) 1999 Russell Cattelan <cattelan@thebarn.com> 6 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 7 * Copyright (c) 1998 by Joachim Kuebart. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgement: 23 * This product includes software developed by Joachim Kuebart. 24 * 25 * 4. The name of the author may not be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * $FreeBSD$ 42 */ 43 44 /* 45 * Part of this code was heavily inspired by the linux driver from 46 * Thomas Sailer (sailer@ife.ee.ethz.ch) 47 * Just about everything has been touched and reworked in some way but 48 * the all the underlying sequences/timing/register values are from 49 * Thomas' code. 50 * 51 */ 52 53 #include <dev/sound/pcm/sound.h> 54 #include <dev/sound/pcm/ac97.h> 55 #include <dev/sound/pci/es137x.h> 56 57 #include <pci/pcireg.h> 58 #include <pci/pcivar.h> 59 60 #include <sys/sysctl.h> 61 62 static int debug = 0; 63 SYSCTL_INT(_debug, OID_AUTO, es_debug, CTLFLAG_RW, &debug, 0, ""); 64 65 #define MEM_MAP_REG 0x14 66 67 /* PCI IDs of supported chips */ 68 #define ES1370_PCI_ID 0x50001274 69 #define ES1371_PCI_ID 0x13711274 70 #define ES1371_PCI_ID2 0x13713274 71 #define ES1371_PCI_ID3 0x58801274 72 73 #define ES_BUFFSIZE 4096 74 75 /* device private data */ 76 struct es_info; 77 78 struct es_chinfo { 79 struct es_info *parent; 80 pcm_channel *channel; 81 snd_dbuf *buffer; 82 int dir, num; 83 u_int32_t fmt; 84 }; 85 86 struct es_info { 87 bus_space_tag_t st; 88 bus_space_handle_t sh; 89 bus_dma_tag_t parent_dmat; 90 91 struct resource *reg, *irq; 92 int regtype, regid, irqid; 93 void *ih; 94 95 device_t dev; 96 int num; 97 /* Contents of board's registers */ 98 u_long ctrl; 99 u_long sctrl; 100 struct es_chinfo pch, rch; 101 }; 102 103 /* -------------------------------------------------------------------- */ 104 105 /* prototypes */ 106 static void es_intr(void *); 107 108 static void es1371_wrcodec(void *, int, u_int32_t); 109 static u_int32_t es1371_rdcodec(void *, int); 110 static u_int es1371_wait_src_ready(struct es_info *); 111 static void es1371_src_write(struct es_info *, u_short, unsigned short); 112 static u_int es1371_adc_rate(struct es_info *, u_int, int); 113 static u_int es1371_dac_rate(struct es_info *, u_int, int); 114 static int es1371_init(struct es_info *es, int); 115 static int es1370_init(struct es_info *); 116 static int es1370_wrcodec(struct es_info *, u_char, u_char); 117 118 /* channel interface */ 119 static void *eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 120 static int eschan_setdir(void *data, int dir); 121 static int eschan_setformat(void *data, u_int32_t format); 122 static int eschan1370_setspeed(void *data, u_int32_t speed); 123 static int eschan1371_setspeed(void *data, u_int32_t speed); 124 static int eschan_setblocksize(void *data, u_int32_t blocksize); 125 static int eschan_trigger(void *data, int go); 126 static int eschan_getptr(void *data); 127 static pcmchan_caps *eschan_getcaps(void *data); 128 129 static u_int32_t es_playfmt[] = { 130 AFMT_U8, 131 AFMT_STEREO | AFMT_U8, 132 AFMT_S16_LE, 133 AFMT_STEREO | AFMT_S16_LE, 134 0 135 }; 136 static pcmchan_caps es_playcaps = {4000, 48000, es_playfmt, 0}; 137 138 static u_int32_t es_recfmt[] = { 139 AFMT_U8, 140 AFMT_STEREO | AFMT_U8, 141 AFMT_S16_LE, 142 AFMT_STEREO | AFMT_S16_LE, 143 0 144 }; 145 static pcmchan_caps es_reccaps = {4000, 48000, es_recfmt, 0}; 146 147 static pcm_channel es1370_chantemplate = { 148 eschan_init, 149 eschan_setdir, 150 eschan_setformat, 151 eschan1370_setspeed, 152 eschan_setblocksize, 153 eschan_trigger, 154 eschan_getptr, 155 eschan_getcaps, 156 NULL, /* free */ 157 NULL, /* nop1 */ 158 NULL, /* nop2 */ 159 NULL, /* nop3 */ 160 NULL, /* nop4 */ 161 NULL, /* nop5 */ 162 NULL, /* nop6 */ 163 NULL, /* nop7 */ 164 }; 165 166 static pcm_channel es1371_chantemplate = { 167 eschan_init, 168 eschan_setdir, 169 eschan_setformat, 170 eschan1371_setspeed, 171 eschan_setblocksize, 172 eschan_trigger, 173 eschan_getptr, 174 eschan_getcaps, 175 NULL, /* free */ 176 NULL, /* nop1 */ 177 NULL, /* nop2 */ 178 NULL, /* nop3 */ 179 NULL, /* nop4 */ 180 NULL, /* nop5 */ 181 NULL, /* nop6 */ 182 NULL, /* nop7 */ 183 }; 184 185 /* -------------------------------------------------------------------- */ 186 187 /* The es1370 mixer interface */ 188 189 static int es1370_mixinit(snd_mixer *m); 190 static int es1370_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right); 191 static int es1370_mixsetrecsrc(snd_mixer *m, u_int32_t src); 192 193 static snd_mixer es1370_mixer = { 194 "AudioPCI 1370 mixer", 195 es1370_mixinit, 196 NULL, 197 NULL, 198 es1370_mixset, 199 es1370_mixsetrecsrc, 200 }; 201 202 static const struct { 203 unsigned volidx:4; 204 unsigned left:4; 205 unsigned right:4; 206 unsigned stereo:1; 207 unsigned recmask:13; 208 unsigned avail:1; 209 } mixtable[SOUND_MIXER_NRDEVICES] = { 210 [SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x0000, 1 }, 211 [SOUND_MIXER_PCM] = { 1, 0x2, 0x3, 1, 0x0400, 1 }, 212 [SOUND_MIXER_SYNTH] = { 2, 0x4, 0x5, 1, 0x0060, 1 }, 213 [SOUND_MIXER_CD] = { 3, 0x6, 0x7, 1, 0x0006, 1 }, 214 [SOUND_MIXER_LINE] = { 4, 0x8, 0x9, 1, 0x0018, 1 }, 215 [SOUND_MIXER_LINE1] = { 5, 0xa, 0xb, 1, 0x1800, 1 }, 216 [SOUND_MIXER_LINE2] = { 6, 0xc, 0x0, 0, 0x0100, 1 }, 217 [SOUND_MIXER_LINE3] = { 7, 0xd, 0x0, 0, 0x0200, 1 }, 218 [SOUND_MIXER_MIC] = { 8, 0xe, 0x0, 0, 0x0001, 1 }, 219 [SOUND_MIXER_OGAIN] = { 9, 0xf, 0x0, 0, 0x0000, 1 } 220 }; 221 222 static int 223 es1370_mixinit(snd_mixer *m) 224 { 225 int i; 226 u_int32_t v; 227 228 v = 0; 229 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 230 if (mixtable[i].avail) v |= (1 << i); 231 mix_setdevs(m, v); 232 v = 0; 233 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 234 if (mixtable[i].recmask) v |= (1 << i); 235 mix_setrecdevs(m, v); 236 return 0; 237 } 238 239 static int 240 es1370_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right) 241 { 242 int l, r, rl, rr; 243 244 if (!mixtable[dev].avail) return -1; 245 l = left; 246 r = mixtable[dev].stereo? right : l; 247 if (mixtable[dev].left == 0xf) { 248 rl = (l < 2)? 0x80 : 7 - (l - 2) / 14; 249 } else { 250 rl = (l < 10)? 0x80 : 15 - (l - 10) / 6; 251 } 252 if (mixtable[dev].stereo) { 253 rr = (r < 10)? 0x80 : 15 - (r - 10) / 6; 254 es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].right, rr); 255 } 256 es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].left, rl); 257 return l | (r << 8); 258 } 259 260 static int 261 es1370_mixsetrecsrc(snd_mixer *m, u_int32_t src) 262 { 263 int i, j = 0; 264 265 if (src == 0) src = 1 << SOUND_MIXER_MIC; 266 src &= mix_getrecdevs(m); 267 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 268 if ((src & (1 << i)) != 0) j |= mixtable[i].recmask; 269 270 es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX1, j & 0x55); 271 es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX1, j & 0xaa); 272 es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX2, (j >> 8) & 0x17); 273 es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX2, (j >> 8) & 0x0f); 274 es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX1, 0x7f); 275 es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX2, 0x3f); 276 return src; 277 } 278 279 static int 280 es1370_wrcodec(struct es_info *es, u_char i, u_char data) 281 { 282 int wait = 100; /* 100 msec timeout */ 283 284 do { 285 if ((bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS) & 286 STAT_CSTAT) == 0) { 287 bus_space_write_2(es->st, es->sh, ES1370_REG_CODEC, 288 ((u_short)i << CODEC_INDEX_SHIFT) | data); 289 return 0; 290 } 291 DELAY(1000); 292 } while (--wait); 293 printf("pcm: es1370_wrcodec timed out\n"); 294 return -1; 295 } 296 297 /* -------------------------------------------------------------------- */ 298 299 /* channel interface */ 300 static void * 301 eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 302 { 303 struct es_info *es = devinfo; 304 struct es_chinfo *ch = (dir == PCMDIR_PLAY)? &es->pch : &es->rch; 305 306 ch->parent = es; 307 ch->channel = c; 308 ch->buffer = b; 309 ch->buffer->bufsize = ES_BUFFSIZE; 310 ch->num = ch->parent->num++; 311 if (chn_allocbuf(ch->buffer, es->parent_dmat) == -1) return NULL; 312 return ch; 313 } 314 315 static int 316 eschan_setdir(void *data, int dir) 317 { 318 struct es_chinfo *ch = data; 319 struct es_info *es = ch->parent; 320 321 if (dir == PCMDIR_PLAY) { 322 bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE, 323 ES1370_REG_DAC2_FRAMEADR >> 8); 324 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMEADR & 0xff, 325 vtophys(ch->buffer->buf)); 326 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff, 327 (ch->buffer->bufsize >> 2) - 1); 328 } else { 329 bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE, 330 ES1370_REG_ADC_FRAMEADR >> 8); 331 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMEADR & 0xff, 332 vtophys(ch->buffer->buf)); 333 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff, 334 (ch->buffer->bufsize >> 2) - 1); 335 } 336 ch->dir = dir; 337 return 0; 338 } 339 340 static int 341 eschan_setformat(void *data, u_int32_t format) 342 { 343 struct es_chinfo *ch = data; 344 struct es_info *es = ch->parent; 345 346 if (ch->dir == PCMDIR_PLAY) { 347 es->sctrl &= ~SCTRL_P2FMT; 348 if (format & AFMT_S16_LE) es->sctrl |= SCTRL_P2SEB; 349 if (format & AFMT_STEREO) es->sctrl |= SCTRL_P2SMB; 350 } else { 351 es->sctrl &= ~SCTRL_R1FMT; 352 if (format & AFMT_S16_LE) es->sctrl |= SCTRL_R1SEB; 353 if (format & AFMT_STEREO) es->sctrl |= SCTRL_R1SMB; 354 } 355 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl); 356 ch->fmt = format; 357 return 0; 358 } 359 360 static int 361 eschan1370_setspeed(void *data, u_int32_t speed) 362 { 363 struct es_chinfo *ch = data; 364 struct es_info *es = ch->parent; 365 366 es->ctrl &= ~CTRL_PCLKDIV; 367 es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV; 368 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl); 369 /* rec/play speeds locked together - should indicate in flags */ 370 return speed; /* XXX calc real speed */ 371 } 372 373 int 374 eschan1371_setspeed(void *data, u_int32_t speed) 375 { 376 struct es_chinfo *ch = data; 377 struct es_info *es = ch->parent; 378 379 if (ch->dir == PCMDIR_PLAY) { 380 return es1371_dac_rate(es, speed, 3 - ch->num); /* play */ 381 } else { 382 return es1371_adc_rate(es, speed, 1); /* record */ 383 } 384 } 385 386 static int 387 eschan_setblocksize(void *data, u_int32_t blocksize) 388 { 389 return blocksize; 390 } 391 392 static int 393 eschan_trigger(void *data, int go) 394 { 395 struct es_chinfo *ch = data; 396 struct es_info *es = ch->parent; 397 unsigned ss, cnt; 398 399 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) 400 return 0; 401 402 ss = 1; 403 ss <<= (ch->fmt & AFMT_STEREO)? 1 : 0; 404 ss <<= (ch->fmt & AFMT_16BIT)? 1 : 0; 405 cnt = ch->buffer->dl / ss - 1; 406 407 if (ch->dir == PCMDIR_PLAY) { 408 if (go == PCMTRIG_START) { 409 int b = (ch->fmt & AFMT_S16_LE)? 2 : 1; 410 es->ctrl |= CTRL_DAC2_EN; 411 es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC | 412 SCTRL_P2LOOPSEL | SCTRL_P2PAUSE | 413 SCTRL_P2DACSEN); 414 es->sctrl |= SCTRL_P2INTEN | (b << SCTRL_SH_P2ENDINC); 415 bus_space_write_4(es->st, es->sh, 416 ES1370_REG_DAC2_SCOUNT, cnt); 417 /* start at beginning of buffer */ 418 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, 419 ES1370_REG_DAC2_FRAMECNT >> 8); 420 bus_space_write_4(es->st, es->sh, 421 ES1370_REG_DAC2_FRAMECNT & 0xff, 422 (ch->buffer->bufsize >> 2) - 1); 423 } else es->ctrl &= ~CTRL_DAC2_EN; 424 } else { 425 if (go == PCMTRIG_START) { 426 es->ctrl |= CTRL_ADC_EN; 427 es->sctrl &= ~SCTRL_R1LOOPSEL; 428 es->sctrl |= SCTRL_R1INTEN; 429 bus_space_write_4(es->st, es->sh, 430 ES1370_REG_ADC_SCOUNT, cnt); 431 /* start at beginning of buffer */ 432 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, 433 ES1370_REG_ADC_FRAMECNT >> 8); 434 bus_space_write_4(es->st, es->sh, 435 ES1370_REG_ADC_FRAMECNT & 0xff, 436 (ch->buffer->bufsize >> 2) - 1); 437 } else es->ctrl &= ~CTRL_ADC_EN; 438 } 439 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl); 440 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl); 441 return 0; 442 } 443 444 static int 445 eschan_getptr(void *data) 446 { 447 struct es_chinfo *ch = data; 448 struct es_info *es = ch->parent; 449 u_int32_t reg, cnt; 450 451 if (ch->dir == PCMDIR_PLAY) 452 reg = ES1370_REG_DAC2_FRAMECNT; 453 else 454 reg = ES1370_REG_ADC_FRAMECNT; 455 456 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, reg >> 8); 457 cnt = bus_space_read_4(es->st, es->sh, reg & 0x000000ff) >> 16; 458 /* cnt is longwords */ 459 return cnt << 2; 460 } 461 462 static pcmchan_caps * 463 eschan_getcaps(void *data) 464 { 465 struct es_chinfo *ch = data; 466 return (ch->dir == PCMDIR_PLAY)? &es_playcaps : &es_reccaps; 467 } 468 469 /* The interrupt handler */ 470 static void 471 es_intr(void *p) 472 { 473 struct es_info *es = p; 474 unsigned intsrc, sctrl; 475 476 intsrc = bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS); 477 if ((intsrc & STAT_INTR) == 0) return; 478 479 sctrl = es->sctrl; 480 if (intsrc & STAT_ADC) sctrl &= ~SCTRL_R1INTEN; 481 if (intsrc & STAT_DAC1) sctrl &= ~SCTRL_P1INTEN; 482 if (intsrc & STAT_DAC2) sctrl &= ~SCTRL_P2INTEN; 483 484 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, sctrl); 485 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl); 486 487 if (intsrc & STAT_ADC) chn_intr(es->rch.channel); 488 if (intsrc & STAT_DAC1); 489 if (intsrc & STAT_DAC2) chn_intr(es->pch.channel); 490 } 491 492 /* ES1370 specific */ 493 static int 494 es1370_init(struct es_info *es) 495 { 496 es->ctrl = CTRL_CDC_EN | CTRL_SERR_DIS | 497 (DAC2_SRTODIV(DSP_DEFAULT_SPEED) << CTRL_SH_PCLKDIV); 498 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl); 499 500 es->sctrl = 0; 501 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl); 502 503 es1370_wrcodec(es, CODEC_RES_PD, 3);/* No RST, PD */ 504 es1370_wrcodec(es, CODEC_CSEL, 0); /* CODEC ADC and CODEC DAC use 505 * {LR,B}CLK2 and run off the LRCLK2 506 * PLL; program DAC_SYNC=0! */ 507 es1370_wrcodec(es, CODEC_ADSEL, 0);/* Recording source is mixer */ 508 es1370_wrcodec(es, CODEC_MGAIN, 0);/* MIC amp is 0db */ 509 510 return 0; 511 } 512 513 /* ES1371 specific */ 514 int 515 es1371_init(struct es_info *es, int rev) 516 { 517 int idx; 518 519 if (debug > 0) printf("es_init\n"); 520 521 es->num = 0; 522 es->ctrl = 0; 523 es->sctrl = 0; 524 /* initialize the chips */ 525 if (rev == 7 || rev >= 9 || rev == 2) { 526 #define ES1371_BINTSUMM_OFF 0x07 527 bus_space_write_4(es->st, es->sh, ES1371_BINTSUMM_OFF, 0x20); 528 if (debug > 0) printf("es_init rev == 7 || rev >= 9\n"); 529 } else { /* pre ac97 2.1 card */ 530 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl); 531 if (debug > 0) printf("es_init pre ac97 2.1\n"); 532 } 533 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl); 534 bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, 0); 535 /* AC'97 warm reset to start the bitclk */ 536 bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, es->ctrl | ES1371_SYNC_RES); 537 DELAY(2000); 538 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->ctrl); 539 /* Init the sample rate converter */ 540 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, ES1371_DIS_SRC); 541 for (idx = 0; idx < 0x80; idx++) 542 es1371_src_write(es, idx, 0); 543 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4); 544 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10); 545 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4); 546 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10); 547 es1371_src_write(es, ES_SMPREG_VOL_ADC, 1 << 12); 548 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, 1 << 12); 549 es1371_src_write(es, ES_SMPREG_VOL_DAC1, 1 << 12); 550 es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1, 1 << 12); 551 es1371_src_write(es, ES_SMPREG_VOL_DAC2, 1 << 12); 552 es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1, 1 << 12); 553 es1371_adc_rate (es, 22050, 1); 554 es1371_dac_rate (es, 22050, 1); 555 es1371_dac_rate (es, 22050, 2); 556 /* WARNING: 557 * enabling the sample rate converter without properly programming 558 * its parameters causes the chip to lock up (the SRC busy bit will 559 * be stuck high, and I've found no way to rectify this other than 560 * power cycle) 561 */ 562 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, 0); 563 564 return (0); 565 } 566 567 static void 568 es1371_wrcodec(void *s, int addr, u_int32_t data) 569 { 570 int sl; 571 unsigned t, x; 572 struct es_info *es = (struct es_info*)s; 573 574 if (debug > 0) printf("wrcodec addr 0x%x data 0x%x\n", addr, data); 575 576 for (t = 0; t < 0x1000; t++) 577 if (!(bus_space_read_4(es->st, es->sh,(ES1371_REG_CODEC & CODEC_WIP)))) 578 break; 579 sl = spltty(); 580 /* save the current state for later */ 581 x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE); 582 /* enable SRC state data in SRC mux */ 583 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, 584 (es1371_wait_src_ready(s) & 585 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1))); 586 /* wait for a SAFE time to write addr/data and then do it, dammit */ 587 for (t = 0; t < 0x1000; t++) 588 if ((bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000) 589 break; 590 591 if (debug > 2) printf("one b_s_w: 0x%x 0x%x 0x%x\n", es->sh, ES1371_REG_CODEC, 592 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) | 593 ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK)); 594 595 bus_space_write_4(es->st, es->sh,ES1371_REG_CODEC, 596 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) | 597 ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK)); 598 /* restore SRC reg */ 599 es1371_wait_src_ready(s); 600 if (debug > 2) printf("two b_s_w: 0x%x 0x%x 0x%x\n", es->sh, ES1371_REG_SMPRATE, x); 601 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x); 602 splx(sl); 603 } 604 605 static u_int32_t 606 es1371_rdcodec(void *s, int addr) 607 { 608 int sl; 609 unsigned t, x; 610 struct es_info *es = (struct es_info *)s; 611 612 if (debug > 0) printf("rdcodec addr 0x%x ... ", addr); 613 614 for (t = 0; t < 0x1000; t++) 615 if (!(x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC) & CODEC_WIP)) 616 break; 617 if (debug > 0) printf("loop 1 t 0x%x x 0x%x ", t, x); 618 619 sl = spltty(); 620 621 /* save the current state for later */ 622 x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE); 623 /* enable SRC state data in SRC mux */ 624 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, 625 (es1371_wait_src_ready(s) & 626 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1))); 627 /* wait for a SAFE time to write addr/data and then do it, dammit */ 628 for (t = 0; t < 0x5000; t++) 629 if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000) 630 break; 631 if (debug > 0) printf("loop 2 t 0x%x x 0x%x ", t, x); 632 bus_space_write_4(es->st, es->sh, ES1371_REG_CODEC, 633 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) | CODEC_PORD); 634 635 /* restore SRC reg */ 636 es1371_wait_src_ready(s); 637 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x); 638 639 splx(sl); 640 641 /* now wait for the stinkin' data (RDY) */ 642 for (t = 0; t < 0x1000; t++) 643 if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC)) & CODEC_RDY) 644 break; 645 if (debug > 0) printf("loop 3 t 0x%x 0x%x ret 0x%x\n", t, x, ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT)); 646 return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT); 647 } 648 649 static u_int 650 es1371_src_read(struct es_info *es, u_short reg) 651 { 652 unsigned int r; 653 654 r = es1371_wait_src_ready(es) & 655 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1); 656 r |= ES1371_SRC_RAM_ADDRO(reg); 657 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,r); 658 return ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es)); 659 } 660 661 static void 662 es1371_src_write(struct es_info *es, u_short reg, u_short data){ 663 u_int r; 664 665 r = es1371_wait_src_ready(es) & 666 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1); 667 r |= ES1371_SRC_RAM_ADDRO(reg) | ES1371_SRC_RAM_DATAO(data); 668 /* printf("es1371_src_write 0x%x 0x%x\n",ES1371_REG_SMPRATE,r | ES1371_SRC_RAM_WE); */ 669 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE); 670 } 671 672 static u_int 673 es1371_adc_rate(struct es_info *es, u_int rate, int set) 674 { 675 u_int n, truncm, freq, result; 676 677 if (rate > 48000) rate = 48000; 678 if (rate < 4000) rate = 4000; 679 n = rate / 3000; 680 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9))) 681 n--; 682 truncm = (21 * n - 1) | 1; 683 freq = ((48000UL << 15) / rate) * n; 684 result = (48000UL << 15) / (freq / n); 685 if (set) { 686 if (rate >= 24000) { 687 if (truncm > 239) truncm = 239; 688 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N, 689 (((239 - truncm) >> 1) << 9) | (n << 4)); 690 } else { 691 if (truncm > 119) truncm = 119; 692 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N, 693 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4)); 694 } 695 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS, 696 (es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) & 697 0x00ff) | ((freq >> 5) & 0xfc00)); 698 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff); 699 es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8); 700 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8); 701 } 702 return result; 703 } 704 705 static u_int 706 es1371_dac_rate(struct es_info *es, u_int rate, int set) 707 { 708 u_int freq, r, result, dac, dis; 709 710 if (rate > 48000) rate = 48000; 711 if (rate < 4000) rate = 4000; 712 freq = (rate << 15) / 3000; 713 result = (freq * 3000) >> 15; 714 if (set) { 715 dac = (set == 1)? ES_SMPREG_DAC1 : ES_SMPREG_DAC2; 716 dis = (set == 1)? ES1371_DIS_P2 : ES1371_DIS_P1; 717 718 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1)); 719 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r); 720 es1371_src_write(es, dac + ES_SMPREG_INT_REGS, 721 (es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) | ((freq >> 5) & 0xfc00)); 722 es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff); 723 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | dis | ES1371_DIS_R1)); 724 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r); 725 } 726 return result; 727 } 728 729 static u_int 730 es1371_wait_src_ready(struct es_info *es) 731 { 732 u_int t, r; 733 734 for (t = 0; t < 500; t++) { 735 if (!((r = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE)) & ES1371_SRC_RAM_BUSY)) 736 return r; 737 DELAY(1000); 738 } 739 printf("es1371: wait src ready timeout 0x%x [0x%x]\n", ES1371_REG_SMPRATE, r); 740 return 0; 741 } 742 743 /* -------------------------------------------------------------------- */ 744 745 /* 746 * Probe and attach the card 747 */ 748 749 static int 750 es_pci_probe(device_t dev) 751 { 752 if (pci_get_devid(dev) == ES1370_PCI_ID) { 753 device_set_desc(dev, "AudioPCI ES1370"); 754 return 0; 755 } else if (pci_get_devid(dev) == ES1371_PCI_ID || 756 pci_get_devid(dev) == ES1371_PCI_ID2 || 757 pci_get_devid(dev) == ES1371_PCI_ID3) { 758 device_set_desc(dev, "AudioPCI ES1371"); 759 return 0; 760 } 761 return ENXIO; 762 } 763 764 static int 765 es_pci_attach(device_t dev) 766 { 767 u_int32_t data; 768 struct es_info *es = 0; 769 int mapped; 770 char status[SND_STATUSLEN]; 771 struct ac97_info *codec = 0; 772 pcm_channel *ct = NULL; 773 774 if ((es = malloc(sizeof *es, M_DEVBUF, M_NOWAIT)) == NULL) { 775 device_printf(dev, "cannot allocate softc\n"); 776 return ENXIO; 777 } 778 bzero(es, sizeof *es); 779 780 es->dev = dev; 781 mapped = 0; 782 data = pci_read_config(dev, PCIR_COMMAND, 2); 783 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 784 pci_write_config(dev, PCIR_COMMAND, data, 2); 785 data = pci_read_config(dev, PCIR_COMMAND, 2); 786 if (mapped == 0 && (data & PCIM_CMD_MEMEN)) { 787 es->regid = MEM_MAP_REG; 788 es->regtype = SYS_RES_MEMORY; 789 es->reg = bus_alloc_resource(dev, es->regtype, &es->regid, 790 0, ~0, 1, RF_ACTIVE); 791 if (es->reg) { 792 es->st = rman_get_bustag(es->reg); 793 es->sh = rman_get_bushandle(es->reg); 794 mapped++; 795 } 796 } 797 if (mapped == 0 && (data & PCIM_CMD_PORTEN)) { 798 es->regid = PCIR_MAPS; 799 es->regtype = SYS_RES_IOPORT; 800 es->reg = bus_alloc_resource(dev, es->regtype, &es->regid, 801 0, ~0, 1, RF_ACTIVE); 802 if (es->reg) { 803 es->st = rman_get_bustag(es->reg); 804 es->sh = rman_get_bushandle(es->reg); 805 mapped++; 806 } 807 } 808 if (mapped == 0) { 809 device_printf(dev, "unable to map register space\n"); 810 goto bad; 811 } 812 813 if (pci_get_devid(dev) == ES1371_PCI_ID || 814 pci_get_devid(dev) == ES1371_PCI_ID2 || 815 pci_get_devid(dev) == ES1371_PCI_ID3) { 816 if(-1 == es1371_init(es, pci_get_revid(dev))) { 817 device_printf(dev, "unable to initialize the card\n"); 818 goto bad; 819 } 820 codec = ac97_create(dev, es, NULL, es1371_rdcodec, es1371_wrcodec); 821 if (codec == NULL) goto bad; 822 /* our init routine does everything for us */ 823 /* set to NULL; flag mixer_init not to run the ac97_init */ 824 /* ac97_mixer.init = NULL; */ 825 if (mixer_init(dev, &ac97_mixer, codec) == -1) goto bad; 826 ct = &es1371_chantemplate; 827 } else if (pci_get_devid(dev) == ES1370_PCI_ID) { 828 if (-1 == es1370_init(es)) { 829 device_printf(dev, "unable to initialize the card\n"); 830 goto bad; 831 } 832 mixer_init(dev, &es1370_mixer, es); 833 ct = &es1370_chantemplate; 834 } else goto bad; 835 836 es->irqid = 0; 837 es->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &es->irqid, 838 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 839 if (!es->irq 840 || bus_setup_intr(dev, es->irq, INTR_TYPE_TTY, es_intr, es, &es->ih)) { 841 device_printf(dev, "unable to map interrupt\n"); 842 goto bad; 843 } 844 845 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 846 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 847 /*highaddr*/BUS_SPACE_MAXADDR, 848 /*filter*/NULL, /*filterarg*/NULL, 849 /*maxsize*/ES_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff, 850 /*flags*/0, &es->parent_dmat) != 0) { 851 device_printf(dev, "unable to create dma tag\n"); 852 goto bad; 853 } 854 855 snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld", 856 (es->regtype == SYS_RES_IOPORT)? "io" : "memory", 857 rman_get_start(es->reg), rman_get_start(es->irq)); 858 859 if (pcm_register(dev, es, 1, 1)) goto bad; 860 pcm_addchan(dev, PCMDIR_REC, ct, es); 861 pcm_addchan(dev, PCMDIR_PLAY, ct, es); 862 pcm_setstatus(dev, status); 863 864 return 0; 865 866 bad: 867 if (codec) ac97_destroy(codec); 868 if (es->reg) bus_release_resource(dev, es->regtype, es->regid, es->reg); 869 if (es->ih) bus_teardown_intr(dev, es->irq, es->ih); 870 if (es->irq) bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq); 871 if (es->parent_dmat) bus_dma_tag_destroy(es->parent_dmat); 872 if (es) free(es, M_DEVBUF); 873 return ENXIO; 874 } 875 876 static int 877 es_pci_detach(device_t dev) 878 { 879 int r; 880 struct es_info *es; 881 882 r = pcm_unregister(dev); 883 if (r) 884 return r; 885 886 es = pcm_getdevinfo(dev); 887 bus_release_resource(dev, es->regtype, es->regid, es->reg); 888 bus_teardown_intr(dev, es->irq, es->ih); 889 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq); 890 bus_dma_tag_destroy(es->parent_dmat); 891 free(es, M_DEVBUF); 892 893 return 0; 894 } 895 896 static device_method_t es_methods[] = { 897 /* Device interface */ 898 DEVMETHOD(device_probe, es_pci_probe), 899 DEVMETHOD(device_attach, es_pci_attach), 900 DEVMETHOD(device_detach, es_pci_detach), 901 902 { 0, 0 } 903 }; 904 905 static driver_t es_driver = { 906 "pcm", 907 es_methods, 908 sizeof(snddev_info), 909 }; 910 911 static devclass_t pcm_devclass; 912 913 DRIVER_MODULE(snd_es137x, pci, es_driver, pcm_devclass, 0, 0); 914 MODULE_DEPEND(snd_es137x, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER); 915 MODULE_VERSION(snd_es137x, 1); 916