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