2 * Support the ENSONIQ AudioPCI board and Creative Labs SoundBlaster PCI
3 * boards based on the ES1370, ES1371 and ES1373 chips.
5 * Copyright (c) 1999 Russell Cattelan <cattelan@thebarn.com>
6 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
7 * Copyright (c) 1998 by Joachim Kuebart. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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.
25 * 4. The name of the author may not be used to endorse or promote
26 * products derived from this software without specific prior
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.
43 * Part of this code was heavily inspired by the linux driver from
44 * Thomas Sailer (sailer@ife.ee.ethz.ch)
45 * Just about everything has been touched and reworked in some way but
46 * the all the underlying sequences/timing/register values are from
51 #include <dev/sound/pcm/sound.h>
52 #include <dev/sound/pcm/ac97.h>
53 #include <dev/sound/pci/es137x.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
58 #include <sys/sysctl.h>
62 SND_DECLARE_FILE("$FreeBSD$");
65 SYSCTL_INT(_debug, OID_AUTO, es_debug, CTLFLAG_RW, &debug, 0, "");
67 #define MEM_MAP_REG 0x14
69 /* PCI IDs of supported chips */
70 #define ES1370_PCI_ID 0x50001274
71 #define ES1371_PCI_ID 0x13711274
72 #define ES1371_PCI_ID2 0x13713274
73 #define CT5880_PCI_ID 0x58801274
74 #define CT4730_PCI_ID 0x89381102
76 #define ES1371REV_ES1371_A 0x02
77 #define ES1371REV_ES1371_B 0x09
79 #define ES1371REV_ES1373_8 0x08
80 #define ES1371REV_ES1373_A 0x04
81 #define ES1371REV_ES1373_B 0x06
83 #define ES1371REV_CT5880_A 0x07
85 #define CT5880REV_CT5880_C 0x02
86 #define CT5880REV_CT5880_D 0x03
87 #define CT5880REV_CT5880_E 0x04
89 #define CT4730REV_CT4730_A 0x00
91 #define ES_DEFAULT_BUFSZ 4096
93 /* device private data */
97 struct es_info *parent;
98 struct pcm_channel *channel;
99 struct snd_dbuf *buffer;
101 u_int32_t fmt, blksz, bufsz;
106 bus_space_handle_t sh;
107 bus_dma_tag_t parent_dmat;
109 struct resource *reg, *irq;
110 int regtype, regid, irqid;
117 /* Contents of board's registers */
120 struct es_chinfo pch, rch;
123 /* -------------------------------------------------------------------- */
126 static void es_intr(void *);
128 static u_int es1371_wait_src_ready(struct es_info *);
129 static void es1371_src_write(struct es_info *, u_short, unsigned short);
130 static u_int es1371_adc_rate(struct es_info *, u_int, int);
131 static u_int es1371_dac_rate(struct es_info *, u_int, int);
132 static int es1371_init(struct es_info *, device_t);
133 static int es1370_init(struct es_info *);
134 static int es1370_wrcodec(struct es_info *, u_char, u_char);
136 static u_int32_t es_playfmt[] = {
138 AFMT_STEREO | AFMT_U8,
140 AFMT_STEREO | AFMT_S16_LE,
143 static struct pcmchan_caps es_playcaps = {4000, 48000, es_playfmt, 0};
145 static u_int32_t es_recfmt[] = {
147 AFMT_STEREO | AFMT_U8,
149 AFMT_STEREO | AFMT_S16_LE,
152 static struct pcmchan_caps es_reccaps = {4000, 48000, es_recfmt, 0};
154 static const struct {
161 } mixtable[SOUND_MIXER_NRDEVICES] = {
162 [SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x0000, 1 },
163 [SOUND_MIXER_PCM] = { 1, 0x2, 0x3, 1, 0x0400, 1 },
164 [SOUND_MIXER_SYNTH] = { 2, 0x4, 0x5, 1, 0x0060, 1 },
165 [SOUND_MIXER_CD] = { 3, 0x6, 0x7, 1, 0x0006, 1 },
166 [SOUND_MIXER_LINE] = { 4, 0x8, 0x9, 1, 0x0018, 1 },
167 [SOUND_MIXER_LINE1] = { 5, 0xa, 0xb, 1, 0x1800, 1 },
168 [SOUND_MIXER_LINE2] = { 6, 0xc, 0x0, 0, 0x0100, 1 },
169 [SOUND_MIXER_LINE3] = { 7, 0xd, 0x0, 0, 0x0200, 1 },
170 [SOUND_MIXER_MIC] = { 8, 0xe, 0x0, 0, 0x0001, 1 },
171 [SOUND_MIXER_OGAIN] = { 9, 0xf, 0x0, 0, 0x0000, 1 }
174 /* -------------------------------------------------------------------- */
175 /* The es1370 mixer interface */
178 es1370_mixinit(struct snd_mixer *m)
184 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
185 if (mixtable[i].avail) v |= (1 << i);
188 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
189 if (mixtable[i].recmask) v |= (1 << i);
190 mix_setrecdevs(m, v);
195 es1370_mixset(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
199 if (!mixtable[dev].avail) return -1;
201 r = mixtable[dev].stereo? right : l;
202 if (mixtable[dev].left == 0xf) {
203 rl = (l < 2)? 0x80 : 7 - (l - 2) / 14;
205 rl = (l < 10)? 0x80 : 15 - (l - 10) / 6;
207 if (mixtable[dev].stereo) {
208 rr = (r < 10)? 0x80 : 15 - (r - 10) / 6;
209 es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].right, rr);
211 es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].left, rl);
216 es1370_mixsetrecsrc(struct snd_mixer *m, u_int32_t src)
220 if (src == 0) src = 1 << SOUND_MIXER_MIC;
221 src &= mix_getrecdevs(m);
222 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
223 if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
225 es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX1, j & 0x55);
226 es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX1, j & 0xaa);
227 es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX2, (j >> 8) & 0x17);
228 es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX2, (j >> 8) & 0x0f);
229 es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX1, 0x7f);
230 es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX2, 0x3f);
234 static kobj_method_t es1370_mixer_methods[] = {
235 KOBJMETHOD(mixer_init, es1370_mixinit),
236 KOBJMETHOD(mixer_set, es1370_mixset),
237 KOBJMETHOD(mixer_setrecsrc, es1370_mixsetrecsrc),
240 MIXER_DECLARE(es1370_mixer);
242 /* -------------------------------------------------------------------- */
245 es1370_wrcodec(struct es_info *es, u_char i, u_char data)
247 int wait = 100; /* 100 msec timeout */
250 if ((bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS) &
252 bus_space_write_2(es->st, es->sh, ES1370_REG_CODEC,
253 ((u_short)i << CODEC_INDEX_SHIFT) | data);
258 printf("pcm: es1370_wrcodec timed out\n");
262 /* -------------------------------------------------------------------- */
264 /* channel interface */
266 eschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
268 struct es_info *es = devinfo;
269 struct es_chinfo *ch = (dir == PCMDIR_PLAY)? &es->pch : &es->rch;
274 ch->bufsz = es->bufsz;
275 ch->blksz = ch->bufsz / 2;
276 ch->num = ch->parent->num++;
277 if (sndbuf_alloc(ch->buffer, es->parent_dmat, ch->bufsz) != 0)
283 eschan_setdir(kobj_t obj, void *data, int dir)
285 struct es_chinfo *ch = data;
286 struct es_info *es = ch->parent;
288 if (dir == PCMDIR_PLAY) {
289 bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE, ES1370_REG_DAC2_FRAMEADR >> 8);
290 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMEADR & 0xff, sndbuf_getbufaddr(ch->buffer));
291 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff, (ch->bufsz >> 2) - 1);
293 bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE, ES1370_REG_ADC_FRAMEADR >> 8);
294 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMEADR & 0xff, sndbuf_getbufaddr(ch->buffer));
295 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff, (ch->bufsz >> 2) - 1);
302 eschan_setformat(kobj_t obj, void *data, u_int32_t format)
304 struct es_chinfo *ch = data;
305 struct es_info *es = ch->parent;
307 if (ch->dir == PCMDIR_PLAY) {
308 es->sctrl &= ~SCTRL_P2FMT;
309 if (format & AFMT_S16_LE) es->sctrl |= SCTRL_P2SEB;
310 if (format & AFMT_STEREO) es->sctrl |= SCTRL_P2SMB;
312 es->sctrl &= ~SCTRL_R1FMT;
313 if (format & AFMT_S16_LE) es->sctrl |= SCTRL_R1SEB;
314 if (format & AFMT_STEREO) es->sctrl |= SCTRL_R1SMB;
316 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
322 eschan1370_setspeed(kobj_t obj, void *data, u_int32_t speed)
324 struct es_chinfo *ch = data;
325 struct es_info *es = ch->parent;
327 es->ctrl &= ~CTRL_PCLKDIV;
328 es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
329 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
330 /* rec/play speeds locked together - should indicate in flags */
331 return speed; /* XXX calc real speed */
335 eschan1371_setspeed(kobj_t obj, void *data, u_int32_t speed)
337 struct es_chinfo *ch = data;
338 struct es_info *es = ch->parent;
340 if (ch->dir == PCMDIR_PLAY) {
341 return es1371_dac_rate(es, speed, 3 - ch->num); /* play */
343 return es1371_adc_rate(es, speed, 1); /* record */
348 eschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
350 struct es_chinfo *ch = data;
352 ch->blksz = blocksize;
353 ch->bufsz = ch->blksz * 2;
354 sndbuf_resize(ch->buffer, 2, ch->blksz);
360 eschan_trigger(kobj_t obj, void *data, int go)
362 struct es_chinfo *ch = data;
363 struct es_info *es = ch->parent;
366 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
369 cnt = (ch->blksz / sndbuf_getbps(ch->buffer)) - 1;
371 if (ch->dir == PCMDIR_PLAY) {
372 if (go == PCMTRIG_START) {
373 int b = (ch->fmt & AFMT_S16_LE)? 2 : 1;
374 es->ctrl |= CTRL_DAC2_EN;
375 es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC | SCTRL_P2LOOPSEL | SCTRL_P2PAUSE | SCTRL_P2DACSEN);
376 es->sctrl |= SCTRL_P2INTEN | (b << SCTRL_SH_P2ENDINC);
377 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_SCOUNT, cnt);
378 /* start at beginning of buffer */
379 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, ES1370_REG_DAC2_FRAMECNT >> 8);
380 bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff, (ch->bufsz >> 2) - 1);
381 } else es->ctrl &= ~CTRL_DAC2_EN;
383 if (go == PCMTRIG_START) {
384 es->ctrl |= CTRL_ADC_EN;
385 es->sctrl &= ~SCTRL_R1LOOPSEL;
386 es->sctrl |= SCTRL_R1INTEN;
387 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_SCOUNT, cnt);
388 /* start at beginning of buffer */
389 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, ES1370_REG_ADC_FRAMECNT >> 8);
390 bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff, (ch->bufsz >> 2) - 1);
391 } else es->ctrl &= ~CTRL_ADC_EN;
393 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
394 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
399 eschan_getptr(kobj_t obj, void *data)
401 struct es_chinfo *ch = data;
402 struct es_info *es = ch->parent;
405 if (ch->dir == PCMDIR_PLAY)
406 reg = ES1370_REG_DAC2_FRAMECNT;
408 reg = ES1370_REG_ADC_FRAMECNT;
410 bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, reg >> 8);
411 cnt = bus_space_read_4(es->st, es->sh, reg & 0x000000ff) >> 16;
412 /* cnt is longwords */
416 static struct pcmchan_caps *
417 eschan_getcaps(kobj_t obj, void *data)
419 struct es_chinfo *ch = data;
420 return (ch->dir == PCMDIR_PLAY)? &es_playcaps : &es_reccaps;
423 static kobj_method_t eschan1370_methods[] = {
424 KOBJMETHOD(channel_init, eschan_init),
425 KOBJMETHOD(channel_setdir, eschan_setdir),
426 KOBJMETHOD(channel_setformat, eschan_setformat),
427 KOBJMETHOD(channel_setspeed, eschan1370_setspeed),
428 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
429 KOBJMETHOD(channel_trigger, eschan_trigger),
430 KOBJMETHOD(channel_getptr, eschan_getptr),
431 KOBJMETHOD(channel_getcaps, eschan_getcaps),
434 CHANNEL_DECLARE(eschan1370);
436 static kobj_method_t eschan1371_methods[] = {
437 KOBJMETHOD(channel_init, eschan_init),
438 KOBJMETHOD(channel_setdir, eschan_setdir),
439 KOBJMETHOD(channel_setformat, eschan_setformat),
440 KOBJMETHOD(channel_setspeed, eschan1371_setspeed),
441 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
442 KOBJMETHOD(channel_trigger, eschan_trigger),
443 KOBJMETHOD(channel_getptr, eschan_getptr),
444 KOBJMETHOD(channel_getcaps, eschan_getcaps),
447 CHANNEL_DECLARE(eschan1371);
449 /* -------------------------------------------------------------------- */
450 /* The interrupt handler */
454 struct es_info *es = p;
455 unsigned intsrc, sctrl;
457 intsrc = bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS);
458 if ((intsrc & STAT_INTR) == 0) return;
461 if (intsrc & STAT_ADC) sctrl &= ~SCTRL_R1INTEN;
462 if (intsrc & STAT_DAC1) sctrl &= ~SCTRL_P1INTEN;
463 if (intsrc & STAT_DAC2) sctrl &= ~SCTRL_P2INTEN;
465 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, sctrl);
466 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
468 if (intsrc & STAT_ADC) chn_intr(es->rch.channel);
469 if (intsrc & STAT_DAC1);
470 if (intsrc & STAT_DAC2) chn_intr(es->pch.channel);
473 /* ES1370 specific */
475 es1370_init(struct es_info *es)
477 es->ctrl = CTRL_CDC_EN | CTRL_SERR_DIS |
478 (DAC2_SRTODIV(DSP_DEFAULT_SPEED) << CTRL_SH_PCLKDIV);
479 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
482 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
484 es1370_wrcodec(es, CODEC_RES_PD, 3);/* No RST, PD */
485 es1370_wrcodec(es, CODEC_CSEL, 0); /* CODEC ADC and CODEC DAC use
486 * {LR,B}CLK2 and run off the LRCLK2
487 * PLL; program DAC_SYNC=0! */
488 es1370_wrcodec(es, CODEC_ADSEL, 0);/* Recording source is mixer */
489 es1370_wrcodec(es, CODEC_MGAIN, 0);/* MIC amp is 0db */
494 /* ES1371 specific */
496 es1371_init(struct es_info *es, device_t dev)
499 int devid = pci_get_devid(dev);
500 int revid = pci_get_revid(dev);
502 if (debug > 0) printf("es_init\n");
507 /* initialize the chips */
508 if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
509 (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
510 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
511 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
512 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E) ||
513 (devid == CT4730_PCI_ID)) {
514 bus_space_write_4(es->st, es->sh, ES1370_REG_STATUS, 0x20000000);
516 if (debug > 0) device_printf(dev, "ac97 2.1 enabled\n");
517 } else { /* pre ac97 2.1 card */
518 bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
519 if (debug > 0) device_printf(dev, "ac97 pre-2.1 enabled\n");
521 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
522 bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, 0);
523 /* AC'97 warm reset to start the bitclk */
524 bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, es->ctrl | ES1371_SYNC_RES);
526 bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->ctrl);
527 /* Init the sample rate converter */
528 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, ES1371_DIS_SRC);
529 for (idx = 0; idx < 0x80; idx++)
530 es1371_src_write(es, idx, 0);
531 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
532 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
533 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
534 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
535 es1371_src_write(es, ES_SMPREG_VOL_ADC, 1 << 12);
536 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, 1 << 12);
537 es1371_src_write(es, ES_SMPREG_VOL_DAC1, 1 << 12);
538 es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
539 es1371_src_write(es, ES_SMPREG_VOL_DAC2, 1 << 12);
540 es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
541 es1371_adc_rate (es, 22050, 1);
542 es1371_dac_rate (es, 22050, 1);
543 es1371_dac_rate (es, 22050, 2);
545 * enabling the sample rate converter without properly programming
546 * its parameters causes the chip to lock up (the SRC busy bit will
547 * be stuck high, and I've found no way to rectify this other than
550 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, 0);
555 /* -------------------------------------------------------------------- */
558 es1371_wrcd(kobj_t obj, void *s, int addr, u_int32_t data)
562 struct es_info *es = (struct es_info*)s;
564 if (debug > 0) printf("wrcodec addr 0x%x data 0x%x\n", addr, data);
566 for (t = 0; t < 0x1000; t++)
567 if (!(bus_space_read_4(es->st, es->sh,(ES1371_REG_CODEC & CODEC_WIP))))
570 /* save the current state for later */
571 x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE);
572 /* enable SRC state data in SRC mux */
573 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,
574 (es1371_wait_src_ready(s) &
575 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1)));
576 /* wait for a SAFE time to write addr/data and then do it, dammit */
577 for (t = 0; t < 0x1000; t++)
578 if ((bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000)
582 printf("one b_s_w: 0x%lx 0x%x 0x%x\n",
583 rman_get_start(es->reg), ES1371_REG_CODEC,
584 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) |
585 ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK));
587 bus_space_write_4(es->st, es->sh,ES1371_REG_CODEC,
588 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) |
589 ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK));
590 /* restore SRC reg */
591 es1371_wait_src_ready(s);
593 printf("two b_s_w: 0x%lx 0x%x 0x%x\n",
594 rman_get_start(es->reg), ES1371_REG_SMPRATE, x);
595 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x);
602 es1371_rdcd(kobj_t obj, void *s, int addr)
606 struct es_info *es = (struct es_info *)s;
608 if (debug > 0) printf("rdcodec addr 0x%x ... ", addr);
610 for (t = 0; t < 0x1000; t++)
611 if (!(x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC) & CODEC_WIP))
613 if (debug > 0) printf("loop 1 t 0x%x x 0x%x ", t, x);
617 /* save the current state for later */
618 x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE);
619 /* enable SRC state data in SRC mux */
620 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,
621 (es1371_wait_src_ready(s) &
622 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1)));
623 /* wait for a SAFE time to write addr/data and then do it, dammit */
624 for (t = 0; t < 0x5000; t++)
625 if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000)
627 if (debug > 0) printf("loop 2 t 0x%x x 0x%x ", t, x);
628 bus_space_write_4(es->st, es->sh, ES1371_REG_CODEC,
629 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) | CODEC_PORD);
631 /* restore SRC reg */
632 es1371_wait_src_ready(s);
633 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x);
637 /* now wait for the stinkin' data (RDY) */
638 for (t = 0; t < 0x1000; t++)
639 if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC)) & CODEC_RDY)
641 if (debug > 0) printf("loop 3 t 0x%x 0x%x ret 0x%x\n", t, x, ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT));
642 return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT);
645 static kobj_method_t es1371_ac97_methods[] = {
646 KOBJMETHOD(ac97_read, es1371_rdcd),
647 KOBJMETHOD(ac97_write, es1371_wrcd),
650 AC97_DECLARE(es1371_ac97);
652 /* -------------------------------------------------------------------- */
655 es1371_src_read(struct es_info *es, u_short reg)
659 r = es1371_wait_src_ready(es) &
660 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1);
661 r |= ES1371_SRC_RAM_ADDRO(reg);
662 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,r);
663 return ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es));
667 es1371_src_write(struct es_info *es, u_short reg, u_short data){
670 r = es1371_wait_src_ready(es) &
671 (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1);
672 r |= ES1371_SRC_RAM_ADDRO(reg) | ES1371_SRC_RAM_DATAO(data);
673 /* printf("es1371_src_write 0x%x 0x%x\n",ES1371_REG_SMPRATE,r | ES1371_SRC_RAM_WE); */
674 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE);
678 es1371_adc_rate(struct es_info *es, u_int rate, int set)
680 u_int n, truncm, freq, result;
682 if (rate > 48000) rate = 48000;
683 if (rate < 4000) rate = 4000;
685 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
687 truncm = (21 * n - 1) | 1;
688 freq = ((48000UL << 15) / rate) * n;
689 result = (48000UL << 15) / (freq / n);
692 if (truncm > 239) truncm = 239;
693 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
694 (((239 - truncm) >> 1) << 9) | (n << 4));
696 if (truncm > 119) truncm = 119;
697 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
698 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
700 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
701 (es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) &
702 0x00ff) | ((freq >> 5) & 0xfc00));
703 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
704 es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8);
705 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8);
711 es1371_dac_rate(struct es_info *es, u_int rate, int set)
713 u_int freq, r, result, dac, dis;
715 if (rate > 48000) rate = 48000;
716 if (rate < 4000) rate = 4000;
717 freq = (rate << 15) / 3000;
718 result = (freq * 3000) >> 15;
720 dac = (set == 1)? ES_SMPREG_DAC1 : ES_SMPREG_DAC2;
721 dis = (set == 1)? ES1371_DIS_P2 : ES1371_DIS_P1;
723 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1));
724 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r);
725 es1371_src_write(es, dac + ES_SMPREG_INT_REGS,
726 (es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) | ((freq >> 5) & 0xfc00));
727 es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
728 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | dis | ES1371_DIS_R1));
729 bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r);
735 es1371_wait_src_ready(struct es_info *es)
739 for (t = 0; t < 500; t++) {
740 if (!((r = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE)) & ES1371_SRC_RAM_BUSY))
744 printf("es1371: wait src ready timeout 0x%x [0x%x]\n", ES1371_REG_SMPRATE, r);
748 /* -------------------------------------------------------------------- */
751 * Probe and attach the card
755 es_pci_probe(device_t dev)
757 switch(pci_get_devid(dev)) {
759 device_set_desc(dev, "AudioPCI ES1370");
763 switch(pci_get_revid(dev)) {
764 case ES1371REV_ES1371_A:
765 device_set_desc(dev, "AudioPCI ES1371-A");
768 case ES1371REV_ES1371_B:
769 device_set_desc(dev, "AudioPCI ES1371-B");
772 case ES1371REV_ES1373_A:
773 device_set_desc(dev, "AudioPCI ES1373-A");
776 case ES1371REV_ES1373_B:
777 device_set_desc(dev, "AudioPCI ES1373-B");
780 case ES1371REV_ES1373_8:
781 device_set_desc(dev, "AudioPCI ES1373-8");
784 case ES1371REV_CT5880_A:
785 device_set_desc(dev, "Creative CT5880-A");
789 device_set_desc(dev, "AudioPCI ES1371-?");
790 device_printf(dev, "unknown revision %d -- please report to cg@freebsd.org\n", pci_get_revid(dev));
795 device_set_desc(dev, "Strange AudioPCI ES1371-? (vid=3274)");
796 device_printf(dev, "unknown revision %d -- please report to cg@freebsd.org\n", pci_get_revid(dev));
800 switch(pci_get_revid(dev)) {
801 case CT4730REV_CT4730_A:
802 device_set_desc(dev, "Creative SB AudioPCI CT4730");
805 device_set_desc(dev, "Creative SB AudioPCI CT4730-?");
806 device_printf(dev, "unknown revision %d -- please report to cg@freebsd.org\n", pci_get_revid(dev));
811 switch(pci_get_revid(dev)) {
812 case CT5880REV_CT5880_C:
813 device_set_desc(dev, "Creative CT5880-C");
816 case CT5880REV_CT5880_D:
817 device_set_desc(dev, "Creative CT5880-D");
820 case CT5880REV_CT5880_E:
821 device_set_desc(dev, "Creative CT5880-E");
825 device_set_desc(dev, "Creative CT5880-?");
826 device_printf(dev, "unknown revision %d -- please report to cg@freebsd.org\n", pci_get_revid(dev));
836 es_pci_attach(device_t dev)
839 struct es_info *es = 0;
841 char status[SND_STATUSLEN];
842 struct ac97_info *codec = 0;
843 kobj_class_t ct = NULL;
845 if ((es = malloc(sizeof *es, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
846 device_printf(dev, "cannot allocate softc\n");
852 data = pci_read_config(dev, PCIR_COMMAND, 2);
853 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
854 pci_write_config(dev, PCIR_COMMAND, data, 2);
855 data = pci_read_config(dev, PCIR_COMMAND, 2);
856 if (mapped == 0 && (data & PCIM_CMD_MEMEN)) {
857 es->regid = MEM_MAP_REG;
858 es->regtype = SYS_RES_MEMORY;
859 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
862 es->st = rman_get_bustag(es->reg);
863 es->sh = rman_get_bushandle(es->reg);
867 if (mapped == 0 && (data & PCIM_CMD_PORTEN)) {
868 es->regid = PCIR_BAR(0);
869 es->regtype = SYS_RES_IOPORT;
870 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
873 es->st = rman_get_bustag(es->reg);
874 es->sh = rman_get_bushandle(es->reg);
879 device_printf(dev, "unable to map register space\n");
883 es->bufsz = pcm_getbuffersize(dev, 4096, ES_DEFAULT_BUFSZ, 65536);
885 if (pci_get_devid(dev) == ES1371_PCI_ID ||
886 pci_get_devid(dev) == ES1371_PCI_ID2 ||
887 pci_get_devid(dev) == CT5880_PCI_ID ||
888 pci_get_devid(dev) == CT4730_PCI_ID) {
889 if(-1 == es1371_init(es, dev)) {
890 device_printf(dev, "unable to initialize the card\n");
893 codec = AC97_CREATE(dev, es, es1371_ac97);
894 if (codec == NULL) goto bad;
895 /* our init routine does everything for us */
896 /* set to NULL; flag mixer_init not to run the ac97_init */
897 /* ac97_mixer.init = NULL; */
898 if (mixer_init(dev, ac97_getmixerclass(), codec)) goto bad;
899 ct = &eschan1371_class;
900 } else if (pci_get_devid(dev) == ES1370_PCI_ID) {
901 if (-1 == es1370_init(es)) {
902 device_printf(dev, "unable to initialize the card\n");
905 if (mixer_init(dev, &es1370_mixer_class, es)) goto bad;
906 ct = &eschan1370_class;
910 es->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &es->irqid,
911 RF_ACTIVE | RF_SHAREABLE);
912 if (!es->irq || snd_setup_intr(dev, es->irq, 0, es_intr, es, &es->ih)) {
913 device_printf(dev, "unable to map interrupt\n");
917 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
918 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
919 /*highaddr*/BUS_SPACE_MAXADDR,
920 /*filter*/NULL, /*filterarg*/NULL,
921 /*maxsize*/es->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
922 /*flags*/0, /*lockfunc*/busdma_lock_mutex,
923 /*lockarg*/&Giant, &es->parent_dmat) != 0) {
924 device_printf(dev, "unable to create dma tag\n");
928 snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld %s",
929 (es->regtype == SYS_RES_IOPORT)? "io" : "memory",
930 rman_get_start(es->reg), rman_get_start(es->irq),PCM_KLDSTRING(snd_es137x));
932 if (pcm_register(dev, es, 1, 1)) goto bad;
933 pcm_addchan(dev, PCMDIR_REC, ct, es);
934 pcm_addchan(dev, PCMDIR_PLAY, ct, es);
935 pcm_setstatus(dev, status);
940 if (codec) ac97_destroy(codec);
941 if (es->reg) bus_release_resource(dev, es->regtype, es->regid, es->reg);
942 if (es->ih) bus_teardown_intr(dev, es->irq, es->ih);
943 if (es->irq) bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
944 if (es->parent_dmat) bus_dma_tag_destroy(es->parent_dmat);
945 if (es) free(es, M_DEVBUF);
950 es_pci_detach(device_t dev)
955 r = pcm_unregister(dev);
959 es = pcm_getdevinfo(dev);
960 bus_release_resource(dev, es->regtype, es->regid, es->reg);
961 bus_teardown_intr(dev, es->irq, es->ih);
962 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
963 bus_dma_tag_destroy(es->parent_dmat);
969 static device_method_t es_methods[] = {
970 /* Device interface */
971 DEVMETHOD(device_probe, es_pci_probe),
972 DEVMETHOD(device_attach, es_pci_attach),
973 DEVMETHOD(device_detach, es_pci_detach),
978 static driver_t es_driver = {
984 DRIVER_MODULE(snd_es137x, pci, es_driver, pcm_devclass, 0, 0);
985 MODULE_DEPEND(snd_es137x, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
986 MODULE_VERSION(snd_es137x, 1);