xref: /freebsd/sys/dev/sound/pci/es137x.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 
72 #define ES_BUFFSIZE 4096
73 
74 /* device private data */
75 struct es_info;
76 
77 struct es_chinfo {
78 	struct es_info *parent;
79 	pcm_channel *channel;
80 	snd_dbuf *buffer;
81 	int dir, num;
82 	u_int32_t fmt;
83 };
84 
85 struct es_info {
86 	bus_space_tag_t st;
87 	bus_space_handle_t sh;
88 	bus_dma_tag_t	parent_dmat;
89 
90 	device_t dev;
91 	int num;
92 	/* Contents of board's registers */
93 	u_long		ctrl;
94 	u_long		sctrl;
95 	struct es_chinfo pch, rch;
96 };
97 
98 /* -------------------------------------------------------------------- */
99 
100 /* prototypes */
101 static void     es_intr(void *);
102 
103 static void	es1371_wrcodec(void *, int, u_int32_t);
104 static u_int32_t es1371_rdcodec(void *, int);
105 static u_int	es1371_wait_src_ready(struct es_info *);
106 static void	es1371_src_write(struct es_info *, u_short, unsigned short);
107 static u_int	es1371_adc_rate(struct es_info *, u_int, int);
108 static u_int	es1371_dac_rate(struct es_info *, u_int, int);
109 static int	es1371_init(struct es_info *es, int);
110 static int      es1370_init(struct es_info *);
111 static int      es1370_wrcodec(struct es_info *, u_char, u_char);
112 
113 /* channel interface */
114 static void *eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
115 static int   eschan_setdir(void *data, int dir);
116 static int   eschan_setformat(void *data, u_int32_t format);
117 static int   eschan1370_setspeed(void *data, u_int32_t speed);
118 static int   eschan1371_setspeed(void *data, u_int32_t speed);
119 static int   eschan_setblocksize(void *data, u_int32_t blocksize);
120 static int   eschan_trigger(void *data, int go);
121 static int   eschan_getptr(void *data);
122 static pcmchan_caps *eschan_getcaps(void *data);
123 
124 static pcmchan_caps es_playcaps = {
125 	4000, 48000,
126 	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
127 	AFMT_STEREO | AFMT_S16_LE
128 };
129 
130 static pcmchan_caps es_reccaps = {
131 	4000, 48000,
132 	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
133 	AFMT_STEREO | AFMT_S16_LE
134 };
135 
136 static pcm_channel es1370_chantemplate = {
137 	eschan_init,
138 	eschan_setdir,
139 	eschan_setformat,
140 	eschan1370_setspeed,
141 	eschan_setblocksize,
142 	eschan_trigger,
143 	eschan_getptr,
144 	eschan_getcaps,
145 };
146 
147 static pcm_channel es1371_chantemplate = {
148 	eschan_init,
149 	eschan_setdir,
150 	eschan_setformat,
151 	eschan1371_setspeed,
152 	eschan_setblocksize,
153 	eschan_trigger,
154 	eschan_getptr,
155 	eschan_getcaps,
156 };
157 
158 /* -------------------------------------------------------------------- */
159 
160 /* The es1370 mixer interface */
161 
162 static int es1370_mixinit(snd_mixer *m);
163 static int es1370_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
164 static int es1370_mixsetrecsrc(snd_mixer *m, u_int32_t src);
165 
166 static snd_mixer es1370_mixer = {
167 	"AudioPCI 1370 mixer",
168 	es1370_mixinit,
169 	es1370_mixset,
170 	es1370_mixsetrecsrc,
171 };
172 
173 static const struct {
174 	unsigned        volidx:4;
175 	unsigned        left:4;
176 	unsigned        right:4;
177 	unsigned        stereo:1;
178 	unsigned        recmask:13;
179 	unsigned        avail:1;
180 }       mixtable[SOUND_MIXER_NRDEVICES] = {
181 	[SOUND_MIXER_VOLUME]	= { 0, 0x0, 0x1, 1, 0x0000, 1 },
182 	[SOUND_MIXER_PCM] 	= { 1, 0x2, 0x3, 1, 0x0400, 1 },
183 	[SOUND_MIXER_SYNTH]	= { 2, 0x4, 0x5, 1, 0x0060, 1 },
184 	[SOUND_MIXER_CD]	= { 3, 0x6, 0x7, 1, 0x0006, 1 },
185 	[SOUND_MIXER_LINE]	= { 4, 0x8, 0x9, 1, 0x0018, 1 },
186 	[SOUND_MIXER_LINE1]	= { 5, 0xa, 0xb, 1, 0x1800, 1 },
187 	[SOUND_MIXER_LINE2]	= { 6, 0xc, 0x0, 0, 0x0100, 1 },
188 	[SOUND_MIXER_LINE3]	= { 7, 0xd, 0x0, 0, 0x0200, 1 },
189 	[SOUND_MIXER_MIC]	= { 8, 0xe, 0x0, 0, 0x0001, 1 },
190 	[SOUND_MIXER_OGAIN]	= { 9, 0xf, 0x0, 0, 0x0000, 1 }
191 };
192 
193 static int
194 es1370_mixinit(snd_mixer *m)
195 {
196 	int i;
197 	u_int32_t v;
198 
199 	v = 0;
200 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
201 		if (mixtable[i].avail) v |= (1 << i);
202 	mix_setdevs(m, v);
203 	v = 0;
204 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
205 		if (mixtable[i].recmask) v |= (1 << i);
206 	mix_setrecdevs(m, v);
207 	return 0;
208 }
209 
210 static int
211 es1370_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
212 {
213 	int l, r, rl, rr;
214 
215 	if (!mixtable[dev].avail) return -1;
216 	l = left;
217 	r = mixtable[dev].stereo? right : l;
218 	if (mixtable[dev].left == 0xf) {
219 		rl = (l < 2)? 0x80 : 7 - (l - 2) / 14;
220 	} else {
221 		rl = (l < 10)? 0x80 : 15 - (l - 10) / 6;
222 	}
223 	if (mixtable[dev].stereo) {
224 		rr = (r < 10)? 0x80 : 15 - (r - 10) / 6;
225 		es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].right, rr);
226 	}
227 	es1370_wrcodec(mix_getdevinfo(m), mixtable[dev].left, rl);
228 	return l | (r << 8);
229 }
230 
231 static int
232 es1370_mixsetrecsrc(snd_mixer *m, u_int32_t src)
233 {
234 	int i, j = 0;
235 
236 	if (src == 0) src = 1 << SOUND_MIXER_MIC;
237 	src &= mix_getrecdevs(m);
238 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
239 		if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
240 
241 	es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX1, j & 0x55);
242 	es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX1, j & 0xaa);
243 	es1370_wrcodec(mix_getdevinfo(m), CODEC_LIMIX2, (j >> 8) & 0x17);
244 	es1370_wrcodec(mix_getdevinfo(m), CODEC_RIMIX2, (j >> 8) & 0x0f);
245 	es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX1, 0x7f);
246 	es1370_wrcodec(mix_getdevinfo(m), CODEC_OMIX2, 0x3f);
247 	return src;
248 }
249 
250 static int
251 es1370_wrcodec(struct es_info *es, u_char i, u_char data)
252 {
253 	int		wait = 100;	/* 100 msec timeout */
254 
255 	do {
256 		if ((bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS) &
257 		      STAT_CSTAT) == 0) {
258 			bus_space_write_2(es->st, es->sh, ES1370_REG_CODEC,
259 				((u_short)i << CODEC_INDEX_SHIFT) | data);
260 			return 0;
261 		}
262 		DELAY(1000);
263 	} while (--wait);
264 	printf("pcm: es1370_wrcodec timed out\n");
265 	return -1;
266 }
267 
268 /* -------------------------------------------------------------------- */
269 
270 /* channel interface */
271 static void *
272 eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
273 {
274 	struct es_info *es = devinfo;
275 	struct es_chinfo *ch = (dir == PCMDIR_PLAY)? &es->pch : &es->rch;
276 
277 	ch->parent = es;
278 	ch->channel = c;
279 	ch->buffer = b;
280 	ch->buffer->bufsize = ES_BUFFSIZE;
281 	ch->num = ch->parent->num++;
282 	if (chn_allocbuf(ch->buffer, es->parent_dmat) == -1) return NULL;
283 	return ch;
284 }
285 
286 static int
287 eschan_setdir(void *data, int dir)
288 {
289 	struct es_chinfo *ch = data;
290 	struct es_info *es = ch->parent;
291 
292 	if (dir == PCMDIR_PLAY) {
293 		bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
294 				  ES1370_REG_DAC2_FRAMEADR >> 8);
295 		bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMEADR & 0xff,
296 				  vtophys(ch->buffer->buf));
297 		bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff,
298 				  (ch->buffer->bufsize >> 2) - 1);
299 	} else {
300 		bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
301 				  ES1370_REG_ADC_FRAMEADR >> 8);
302 		bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMEADR & 0xff,
303 				  vtophys(ch->buffer->buf));
304 		bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff,
305 				  (ch->buffer->bufsize >> 2) - 1);
306 	}
307 	ch->dir = dir;
308 	return 0;
309 }
310 
311 static int
312 eschan_setformat(void *data, u_int32_t format)
313 {
314 	struct es_chinfo *ch = data;
315 	struct es_info *es = ch->parent;
316 
317 	if (ch->dir == PCMDIR_PLAY) {
318 		es->sctrl &= ~SCTRL_P2FMT;
319 		if (format & AFMT_S16_LE) es->sctrl |= SCTRL_P2SEB;
320 		if (format & AFMT_STEREO) es->sctrl |= SCTRL_P2SMB;
321 	} else {
322 		es->sctrl &= ~SCTRL_R1FMT;
323 		if (format & AFMT_S16_LE) es->sctrl |= SCTRL_R1SEB;
324 		if (format & AFMT_STEREO) es->sctrl |= SCTRL_R1SMB;
325 	}
326 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
327 	ch->fmt = format;
328 	return 0;
329 }
330 
331 static int
332 eschan1370_setspeed(void *data, u_int32_t speed)
333 {
334 	struct es_chinfo *ch = data;
335 	struct es_info *es = ch->parent;
336 
337 	es->ctrl &= ~CTRL_PCLKDIV;
338 	es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
339 	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
340 	/* rec/play speeds locked together - should indicate in flags */
341 	return speed; /* XXX calc real speed */
342 }
343 
344 int
345 eschan1371_setspeed(void *data, u_int32_t speed)
346 {
347   	struct es_chinfo *ch = data;
348   	struct es_info *es = ch->parent;
349 
350 	if (ch->dir == PCMDIR_PLAY) {
351   		return es1371_dac_rate(es, speed, 3 - ch->num); /* play */
352 	} else {
353   		return es1371_adc_rate(es, speed, 1); /* record */
354 	}
355 }
356 
357 static int
358 eschan_setblocksize(void *data, u_int32_t blocksize)
359 {
360 	return blocksize;
361 }
362 
363 static int
364 eschan_trigger(void *data, int go)
365 {
366 	struct es_chinfo *ch = data;
367 	struct es_info *es = ch->parent;
368 	unsigned cnt = ch->buffer->dl / ch->buffer->sample_size - 1;
369 
370 	if (go == PCMTRIG_EMLDMAWR) return 0;
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 |
376 				       SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
377 				       SCTRL_P2DACSEN);
378 			es->sctrl |= SCTRL_P2INTEN | (b << SCTRL_SH_P2ENDINC);
379 			bus_space_write_4(es->st, es->sh,
380 					  ES1370_REG_DAC2_SCOUNT, cnt);
381 			/* start at beginning of buffer */
382 			bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
383 					  ES1370_REG_DAC2_FRAMECNT >> 8);
384 			bus_space_write_4(es->st, es->sh,
385 					  ES1370_REG_DAC2_FRAMECNT & 0xff,
386 				  	  (ch->buffer->bufsize >> 2) - 1);
387 		} else es->ctrl &= ~CTRL_DAC2_EN;
388 	} else {
389 		if (go == PCMTRIG_START) {
390 			es->ctrl |= CTRL_ADC_EN;
391 			es->sctrl &= ~SCTRL_R1LOOPSEL;
392 			es->sctrl |= SCTRL_R1INTEN;
393 			bus_space_write_4(es->st, es->sh,
394 					  ES1370_REG_ADC_SCOUNT, cnt);
395 			/* start at beginning of buffer */
396 			bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
397 					  ES1370_REG_ADC_FRAMECNT >> 8);
398 			bus_space_write_4(es->st, es->sh,
399 					  ES1370_REG_ADC_FRAMECNT & 0xff,
400 				  	  (ch->buffer->bufsize >> 2) - 1);
401 		} else es->ctrl &= ~CTRL_ADC_EN;
402 	}
403 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
404 	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
405 	return 0;
406 }
407 
408 static int
409 eschan_getptr(void *data)
410 {
411 	struct es_chinfo *ch = data;
412 	struct es_info *es = ch->parent;
413 	u_int32_t reg, cnt;
414 
415 	if (ch->dir == PCMDIR_PLAY)
416 		reg = ES1370_REG_DAC2_FRAMECNT;
417 	else
418 		reg = ES1370_REG_ADC_FRAMECNT;
419 
420 	bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE, reg >> 8);
421 	cnt = bus_space_read_4(es->st, es->sh, reg & 0x000000ff) >> 16;
422 	/* cnt is longwords */
423 	return cnt << 2;
424 }
425 
426 static pcmchan_caps *
427 eschan_getcaps(void *data)
428 {
429 	struct es_chinfo *ch = data;
430 	return (ch->dir == PCMDIR_PLAY)? &es_playcaps : &es_reccaps;
431 }
432 
433 /* The interrupt handler */
434 static void
435 es_intr(void *p)
436 {
437 	struct es_info *es = p;
438 	unsigned	intsrc, sctrl;
439 
440 	intsrc = bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS);
441 	if ((intsrc & STAT_INTR) == 0) return;
442 
443 	sctrl = es->sctrl;
444 	if (intsrc & STAT_ADC)  sctrl &= ~SCTRL_R1INTEN;
445 	if (intsrc & STAT_DAC1)	sctrl &= ~SCTRL_P1INTEN;
446 	if (intsrc & STAT_DAC2)	sctrl &= ~SCTRL_P2INTEN;
447 
448 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, sctrl);
449 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
450 
451 	if (intsrc & STAT_ADC) chn_intr(es->rch.channel);
452 	if (intsrc & STAT_DAC1);
453 	if (intsrc & STAT_DAC2)	chn_intr(es->pch.channel);
454 }
455 
456 /* ES1370 specific */
457 static int
458 es1370_init(struct es_info *es)
459 {
460 	es->ctrl = CTRL_CDC_EN | CTRL_SERR_DIS |
461 		(DAC2_SRTODIV(DSP_DEFAULT_SPEED) << CTRL_SH_PCLKDIV);
462 	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
463 
464 	es->sctrl = 0;
465 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
466 
467 	es1370_wrcodec(es, CODEC_RES_PD, 3);/* No RST, PD */
468 	es1370_wrcodec(es, CODEC_CSEL, 0);	/* CODEC ADC and CODEC DAC use
469 					         * {LR,B}CLK2 and run off the LRCLK2
470 					         * PLL; program DAC_SYNC=0!  */
471 	es1370_wrcodec(es, CODEC_ADSEL, 0);/* Recording source is mixer */
472 	es1370_wrcodec(es, CODEC_MGAIN, 0);/* MIC amp is 0db */
473 
474 	return 0;
475 }
476 
477 /* ES1371 specific */
478 int
479 es1371_init(struct es_info *es, int rev)
480 {
481 	int idx;
482 
483 	if (debug > 0) printf("es_init\n");
484 
485 	es->num = 0;
486 	es->ctrl = 0;
487 	es->sctrl = 0;
488 	/* initialize the chips */
489 	if (rev == 7 || rev >= 9) {
490 #define ES1371_BINTSUMM_OFF 0x07
491 		bus_space_write_4(es->st, es->sh, ES1371_BINTSUMM_OFF, 0x20);
492 		if (debug > 0) printf("es_init rev == 7 || rev >= 9\n");
493 	} else { /* pre ac97 2.1 card */
494 		bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
495 		if (debug > 0) printf("es_init pre ac97 2.1\n");
496 	}
497 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
498 	bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, 0);
499 	/* AC'97 warm reset to start the bitclk */
500 	bus_space_write_4(es->st, es->sh, ES1371_REG_LEGACY, es->ctrl | ES1371_SYNC_RES);
501 	DELAY(2000);
502 	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->ctrl);
503 	/* Init the sample rate converter */
504 	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, ES1371_DIS_SRC);
505 	for (idx = 0; idx < 0x80; idx++)
506 		es1371_src_write(es, idx, 0);
507 	es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N,  16 << 4);
508 	es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
509 	es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N,  16 << 4);
510 	es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
511 	es1371_src_write(es, ES_SMPREG_VOL_ADC,                   1 << 12);
512 	es1371_src_write(es, ES_SMPREG_VOL_ADC  + 1,              1 << 12);
513 	es1371_src_write(es, ES_SMPREG_VOL_DAC1,                  1 << 12);
514 	es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1,              1 << 12);
515 	es1371_src_write(es, ES_SMPREG_VOL_DAC2,                  1 << 12);
516 	es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1,              1 << 12);
517 	es1371_adc_rate (es, 22050,                               1);
518 	es1371_dac_rate (es, 22050,                               1);
519 	es1371_dac_rate (es, 22050,                               2);
520 	/* WARNING:
521 	 * enabling the sample rate converter without properly programming
522 	 * its parameters causes the chip to lock up (the SRC busy bit will
523 	 * be stuck high, and I've found no way to rectify this other than
524 	 * power cycle)
525 	 */
526 	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, 0);
527 
528 	return (0);
529 }
530 
531 static void
532 es1371_wrcodec(void *s, int addr, u_int32_t data)
533 {
534     	int sl;
535     	unsigned t, x;
536 	struct es_info *es = (struct es_info*)s;
537 
538 	if (debug > 0) printf("wrcodec addr 0x%x data 0x%x\n", addr, data);
539 
540 	for (t = 0; t < 0x1000; t++)
541 	  	if (!(bus_space_read_4(es->st, es->sh,(ES1371_REG_CODEC & CODEC_WIP))))
542 			break;
543 	sl = spltty();
544 	/* save the current state for later */
545  	x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE);
546 	/* enable SRC state data in SRC mux */
547 	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,
548 	  	(es1371_wait_src_ready(s) &
549 	   	(ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1)));
550 	/* wait for a SAFE time to write addr/data and then do it, dammit */
551 	for (t = 0; t < 0x1000; t++)
552 	  	if ((bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000)
553 			break;
554 
555 	if (debug > 2) printf("one b_s_w: 0x%x 0x%x 0x%x\n", es->sh, ES1371_REG_CODEC,
556 			 ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) |
557 			 ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK));
558 
559 	bus_space_write_4(es->st, es->sh,ES1371_REG_CODEC,
560 			  ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) |
561 			  ((data << CODEC_PODAT_SHIFT) & CODEC_PODAT_MASK));
562 	/* restore SRC reg */
563 	es1371_wait_src_ready(s);
564 	if (debug > 2) printf("two b_s_w: 0x%x 0x%x 0x%x\n", es->sh, ES1371_REG_SMPRATE, x);
565 	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x);
566 	splx(sl);
567 }
568 
569 static u_int32_t
570 es1371_rdcodec(void *s, int addr)
571 {
572   	int sl;
573   	unsigned t, x;
574   	struct es_info *es = (struct es_info *)s;
575 
576   	if (debug > 0) printf("rdcodec addr 0x%x ... ", addr);
577 
578   	for (t = 0; t < 0x1000; t++)
579 		if (!(x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC) & CODEC_WIP))
580 	  		break;
581    	if (debug > 0) printf("loop 1 t 0x%x x 0x%x ", t, x);
582 
583   	sl = spltty();
584 
585   	/* save the current state for later */
586   	x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE);
587   	/* enable SRC state data in SRC mux */
588   	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,
589 			  (es1371_wait_src_ready(s) &
590 			  (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1)));
591   	/* wait for a SAFE time to write addr/data and then do it, dammit */
592   	for (t = 0; t < 0x5000; t++)
593 		if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE) & 0x00070000) == 0x00010000)
594 	  		break;
595   	if (debug > 0) printf("loop 2 t 0x%x x 0x%x ", t, x);
596   	bus_space_write_4(es->st, es->sh, ES1371_REG_CODEC,
597 			  ((addr << CODEC_POADD_SHIFT) & CODEC_POADD_MASK) | CODEC_PORD);
598 
599   	/* restore SRC reg */
600   	es1371_wait_src_ready(s);
601   	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, x);
602 
603   	splx(sl);
604 
605   	/* now wait for the stinkin' data (RDY) */
606   	for (t = 0; t < 0x1000; t++)
607 		if ((x = bus_space_read_4(es->st, es->sh, ES1371_REG_CODEC)) & CODEC_RDY)
608 	  		break;
609   	if (debug > 0) printf("loop 3 t 0x%x 0x%x ret 0x%x\n", t, x, ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT));
610   	return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT);
611 }
612 
613 static u_int
614 es1371_src_read(struct es_info *es, u_short reg)
615 {
616   	unsigned int r;
617 
618   	r = es1371_wait_src_ready(es) &
619 		(ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1);
620   	r |= ES1371_SRC_RAM_ADDRO(reg);
621   	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE,r);
622   	return ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es));
623 }
624 
625 static void
626 es1371_src_write(struct es_info *es, u_short reg, u_short data){
627 	u_int r;
628 
629 	r = es1371_wait_src_ready(es) &
630 		(ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1);
631 	r |= ES1371_SRC_RAM_ADDRO(reg) |  ES1371_SRC_RAM_DATAO(data);
632 	/*	printf("es1371_src_write 0x%x 0x%x\n",ES1371_REG_SMPRATE,r | ES1371_SRC_RAM_WE); */
633 	bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE);
634 }
635 
636 static u_int
637 es1371_adc_rate(struct es_info *es, u_int rate, int set)
638 {
639   	u_int n, truncm, freq, result;
640 
641   	if (rate > 48000) rate = 48000;
642   	if (rate < 4000) rate = 4000;
643   	n = rate / 3000;
644   	if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
645 		n--;
646   	truncm = (21 * n - 1) | 1;
647   	freq = ((48000UL << 15) / rate) * n;
648   	result = (48000UL << 15) / (freq / n);
649   	if (set) {
650 		if (rate >= 24000) {
651 	  		if (truncm > 239) truncm = 239;
652 	  		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
653 				(((239 - truncm) >> 1) << 9) | (n << 4));
654 		} else {
655 	  		if (truncm > 119) truncm = 119;
656 	  		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
657 				0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
658 		}
659 		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
660 		 	(es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) &
661 		  	0x00ff) | ((freq >> 5) & 0xfc00));
662 		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
663 		es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8);
664 		es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8);
665 	}
666 	return result;
667 }
668 
669 static u_int
670 es1371_dac_rate(struct es_info *es, u_int rate, int set)
671 {
672   	u_int freq, r, result, dac, dis;
673 
674   	if (rate > 48000) rate = 48000;
675   	if (rate < 4000) rate = 4000;
676   	freq = (rate << 15) / 3000;
677   	result = (freq * 3000) >> 15;
678   	if (set) {
679 		dac = (set == 1)? ES_SMPREG_DAC1 : ES_SMPREG_DAC2;
680 		dis = (set == 1)? ES1371_DIS_P2 : ES1371_DIS_P1;
681 
682 		r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 | ES1371_DIS_P2 | ES1371_DIS_R1));
683 		bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r);
684 		es1371_src_write(es, dac + ES_SMPREG_INT_REGS,
685 			 	(es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) | ((freq >> 5) & 0xfc00));
686 		es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
687 		r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | dis | ES1371_DIS_R1));
688 		bus_space_write_4(es->st, es->sh, ES1371_REG_SMPRATE, r);
689   	}
690   	return result;
691 }
692 
693 static u_int
694 es1371_wait_src_ready(struct es_info *es)
695 {
696   	u_int t, r;
697 
698   	for (t = 0; t < 500; t++) {
699 		if (!((r = bus_space_read_4(es->st, es->sh, ES1371_REG_SMPRATE)) & ES1371_SRC_RAM_BUSY))
700 	  		return r;
701 		DELAY(1000);
702   	}
703   	printf("es1371: wait src ready timeout 0x%x [0x%x]\n", ES1371_REG_SMPRATE, r);
704   	return 0;
705 }
706 
707 /* -------------------------------------------------------------------- */
708 
709 /*
710  * Probe and attach the card
711  */
712 
713 static int
714 es_pci_probe(device_t dev)
715 {
716 	if (pci_get_devid(dev) == ES1370_PCI_ID) {
717 		device_set_desc(dev, "AudioPCI ES1370");
718 		return 0;
719 	} else if (pci_get_devid(dev) == ES1371_PCI_ID ||
720 		   pci_get_devid(dev) == ES1371_PCI_ID2) {
721 		device_set_desc(dev, "AudioPCI ES1371");
722 		return 0;
723 	}
724 	return ENXIO;
725 }
726 
727 static int
728 es_pci_attach(device_t dev)
729 {
730 	snddev_info    *d;
731 	u_int32_t	data;
732 	struct es_info *es = 0;
733 	int		type = 0;
734 	int		regid;
735 	struct resource *reg = 0;
736 	int		mapped;
737 	int		irqid;
738 	struct resource *irq = 0;
739 	void		*ih = 0;
740 	char		status[SND_STATUSLEN];
741 	struct ac97_info *codec;
742 	pcm_channel     *ct = NULL;
743 
744 	d = device_get_softc(dev);
745 	if ((es = malloc(sizeof *es, M_DEVBUF, M_NOWAIT)) == NULL) {
746 		device_printf(dev, "cannot allocate softc\n");
747 		return ENXIO;
748 	}
749 	bzero(es, sizeof *es);
750 
751 	es->dev = dev;
752 	mapped = 0;
753 	data = pci_read_config(dev, PCIR_COMMAND, 2);
754 	data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
755 	pci_write_config(dev, PCIR_COMMAND, data, 2);
756 	data = pci_read_config(dev, PCIR_COMMAND, 2);
757 	if (mapped == 0 && (data & PCIM_CMD_MEMEN)) {
758 		regid = MEM_MAP_REG;
759 		type = SYS_RES_MEMORY;
760 		reg = bus_alloc_resource(dev, type, &regid,
761 					 0, ~0, 1, RF_ACTIVE);
762 		if (reg) {
763 			es->st = rman_get_bustag(reg);
764 			es->sh = rman_get_bushandle(reg);
765 			mapped++;
766 		}
767 	}
768 	if (mapped == 0 && (data & PCIM_CMD_PORTEN)) {
769 		regid = PCI_MAP_REG_START;
770 		type = SYS_RES_IOPORT;
771 		reg = bus_alloc_resource(dev, type, &regid,
772 					 0, ~0, 1, RF_ACTIVE);
773 		if (reg) {
774 			es->st = rman_get_bustag(reg);
775 			es->sh = rman_get_bushandle(reg);
776 			mapped++;
777 		}
778 	}
779 	if (mapped == 0) {
780 		device_printf(dev, "unable to map register space\n");
781 		goto bad;
782 	}
783 
784 	if (pci_get_devid(dev) == ES1371_PCI_ID ||
785 	    pci_get_devid(dev) == ES1371_PCI_ID2) {
786 		if(-1 == es1371_init(es, pci_get_revid(dev))) {
787 			device_printf(dev, "unable to initialize the card\n");
788 			goto bad;
789 		}
790 	  	codec = ac97_create(dev, es, NULL, es1371_rdcodec, es1371_wrcodec);
791 	  	if (codec == NULL) goto bad;
792 	  	/* our init routine does everything for us */
793 	  	/* set to NULL; flag mixer_init not to run the ac97_init */
794 	  	/*	  ac97_mixer.init = NULL;  */
795 		if (mixer_init(d, &ac97_mixer, codec) == -1) goto bad;
796 		ct = &es1371_chantemplate;
797 	} else if (pci_get_devid(dev) == ES1370_PCI_ID) {
798 	  	if (-1 == es1370_init(es)) {
799 			device_printf(dev, "unable to initialize the card\n");
800 			goto bad;
801 	  	}
802 	  	mixer_init(d, &es1370_mixer, es);
803 		ct = &es1370_chantemplate;
804 	} else goto bad;
805 
806 	irqid = 0;
807 	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &irqid,
808 				 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
809 	if (!irq
810 	    || bus_setup_intr(dev, irq, INTR_TYPE_TTY, es_intr, es, &ih)) {
811 		device_printf(dev, "unable to map interrupt\n");
812 		goto bad;
813 	}
814 
815 	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
816 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
817 		/*highaddr*/BUS_SPACE_MAXADDR,
818 		/*filter*/NULL, /*filterarg*/NULL,
819 		/*maxsize*/ES_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff,
820 		/*flags*/0, &es->parent_dmat) != 0) {
821 		device_printf(dev, "unable to create dma tag\n");
822 		goto bad;
823 	}
824 
825 	snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld",
826 		 (type == SYS_RES_IOPORT)? "io" : "memory",
827 		 rman_get_start(reg), rman_get_start(irq));
828 
829 	if (pcm_register(dev, es, 1, 1)) goto bad;
830 	pcm_addchan(dev, PCMDIR_REC, ct, es);
831 	pcm_addchan(dev, PCMDIR_PLAY, ct, es);
832 	pcm_setstatus(dev, status);
833 
834 	return 0;
835 
836  bad:
837 	if (es) free(es, M_DEVBUF);
838 	if (reg) bus_release_resource(dev, type, regid, reg);
839 	if (ih) bus_teardown_intr(dev, irq, ih);
840 	if (irq) bus_release_resource(dev, SYS_RES_IRQ, irqid, irq);
841 	return ENXIO;
842 }
843 
844 static device_method_t es_methods[] = {
845 	/* Device interface */
846 	DEVMETHOD(device_probe,		es_pci_probe),
847 	DEVMETHOD(device_attach,	es_pci_attach),
848 
849 	{ 0, 0 }
850 };
851 
852 static driver_t es_driver = {
853 	"pcm",
854 	es_methods,
855 	sizeof(snddev_info),
856 };
857 
858 static devclass_t pcm_devclass;
859 
860 DRIVER_MODULE(es, pci, es_driver, pcm_devclass, 0, 0);
861