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