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