1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifdef HAVE_KERNEL_OPTION_HEADERS
29 #include "opt_snd.h"
30 #endif
31
32 #include <dev/sound/pcm/sound.h>
33
34 #include <dev/pci/pcireg.h>
35 #include <dev/pci/pcivar.h>
36
37 #include <dev/sound/isa/sb.h>
38
39 #include "mixer_if.h"
40
41 #define SOLO_DEFAULT_BUFSZ 16384
42 #define ABS(x) (((x) < 0)? -(x) : (x))
43
44 /* if defined, playback always uses the 2nd channel and full duplex works */
45 #define ESS18XX_DUPLEX 1
46
47 /* more accurate clocks and split audio1/audio2 rates */
48 #define ESS18XX_NEWSPEED
49
50 static u_int32_t ess_playfmt[] = {
51 SND_FORMAT(AFMT_U8, 1, 0),
52 SND_FORMAT(AFMT_U8, 2, 0),
53 SND_FORMAT(AFMT_S8, 1, 0),
54 SND_FORMAT(AFMT_S8, 2, 0),
55 SND_FORMAT(AFMT_S16_LE, 1, 0),
56 SND_FORMAT(AFMT_S16_LE, 2, 0),
57 SND_FORMAT(AFMT_U16_LE, 1, 0),
58 SND_FORMAT(AFMT_U16_LE, 2, 0),
59 0
60 };
61 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0};
62
63 /*
64 * Recording output is byte-swapped
65 */
66 static u_int32_t ess_recfmt[] = {
67 SND_FORMAT(AFMT_U8, 1, 0),
68 SND_FORMAT(AFMT_U8, 2, 0),
69 SND_FORMAT(AFMT_S8, 1, 0),
70 SND_FORMAT(AFMT_S8, 2, 0),
71 SND_FORMAT(AFMT_S16_BE, 1, 0),
72 SND_FORMAT(AFMT_S16_BE, 2, 0),
73 SND_FORMAT(AFMT_U16_BE, 1, 0),
74 SND_FORMAT(AFMT_U16_BE, 2, 0),
75 0
76 };
77 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0};
78
79 struct ess_info;
80
81 struct ess_chinfo {
82 struct ess_info *parent;
83 struct pcm_channel *channel;
84 struct snd_dbuf *buffer;
85 int dir, hwch, stopping;
86 u_int32_t fmt, spd, blksz;
87 };
88
89 struct ess_info {
90 struct resource *io, *sb, *vc, *mpu, *gp; /* I/O address for the board */
91 struct resource *irq;
92 void *ih;
93 bus_dma_tag_t parent_dmat;
94
95 int simplex_dir, type, dmasz[2];
96 unsigned int duplex:1, newspeed:1;
97 unsigned int bufsz;
98
99 struct ess_chinfo pch, rch;
100 struct mtx *lock;
101 };
102
103 #define ess_lock(_ess) snd_mtxlock((_ess)->lock)
104 #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock)
105 #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock)
106
107 static int ess_rd(struct ess_info *sc, int reg);
108 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
109 static int ess_dspready(struct ess_info *sc);
110 static int ess_cmd(struct ess_info *sc, u_char val);
111 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
112 static int ess_get_byte(struct ess_info *sc);
113 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
114 static int ess_getmixer(struct ess_info *sc, u_int port);
115 static int ess_reset_dsp(struct ess_info *sc);
116
117 static int ess_write(struct ess_info *sc, u_char reg, int val);
118 static int ess_read(struct ess_info *sc, u_char reg);
119
120 static void ess_intr(void *arg);
121 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
122 static int ess_start(struct ess_chinfo *ch);
123 static int ess_stop(struct ess_chinfo *ch);
124
125 static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir);
126 static int ess_dmapos(struct ess_info *sc, int ch);
127 static int ess_dmatrigger(struct ess_info *sc, int ch, int go);
128
129 /*
130 * Common code for the midi and pcm functions
131 *
132 * ess_cmd write a single byte to the CMD port.
133 * ess_cmd1 write a CMD + 1 byte arg
134 * ess_cmd2 write a CMD + 2 byte arg
135 * ess_get_byte returns a single byte from the DSP data port
136 *
137 * ess_write is actually ess_cmd1
138 * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
139 */
140
141 static int
port_rd(struct resource * port,int regno,int size)142 port_rd(struct resource *port, int regno, int size)
143 {
144 bus_space_tag_t st = rman_get_bustag(port);
145 bus_space_handle_t sh = rman_get_bushandle(port);
146
147 switch (size) {
148 case 1:
149 return bus_space_read_1(st, sh, regno);
150 case 2:
151 return bus_space_read_2(st, sh, regno);
152 case 4:
153 return bus_space_read_4(st, sh, regno);
154 default:
155 return 0xffffffff;
156 }
157 }
158
159 static void
port_wr(struct resource * port,int regno,u_int32_t data,int size)160 port_wr(struct resource *port, int regno, u_int32_t data, int size)
161 {
162 bus_space_tag_t st = rman_get_bustag(port);
163 bus_space_handle_t sh = rman_get_bushandle(port);
164
165 switch (size) {
166 case 1:
167 bus_space_write_1(st, sh, regno, data);
168 break;
169 case 2:
170 bus_space_write_2(st, sh, regno, data);
171 break;
172 case 4:
173 bus_space_write_4(st, sh, regno, data);
174 break;
175 }
176 }
177
178 static int
ess_rd(struct ess_info * sc,int reg)179 ess_rd(struct ess_info *sc, int reg)
180 {
181 return port_rd(sc->sb, reg, 1);
182 }
183
184 static void
ess_wr(struct ess_info * sc,int reg,u_int8_t val)185 ess_wr(struct ess_info *sc, int reg, u_int8_t val)
186 {
187 port_wr(sc->sb, reg, val, 1);
188 }
189
190 static int
ess_dspready(struct ess_info * sc)191 ess_dspready(struct ess_info *sc)
192 {
193 return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
194 }
195
196 static int
ess_dspwr(struct ess_info * sc,u_char val)197 ess_dspwr(struct ess_info *sc, u_char val)
198 {
199 int i;
200
201 for (i = 0; i < 1000; i++) {
202 if (ess_dspready(sc)) {
203 ess_wr(sc, SBDSP_CMD, val);
204 return 1;
205 }
206 if (i > 10) DELAY((i > 100)? 1000 : 10);
207 }
208 printf("ess_dspwr(0x%02x) timed out.\n", val);
209 return 0;
210 }
211
212 static int
ess_cmd(struct ess_info * sc,u_char val)213 ess_cmd(struct ess_info *sc, u_char val)
214 {
215 DEB(printf("ess_cmd: %x\n", val));
216 return ess_dspwr(sc, val);
217 }
218
219 static int
ess_cmd1(struct ess_info * sc,u_char cmd,int val)220 ess_cmd1(struct ess_info *sc, u_char cmd, int val)
221 {
222 DEB(printf("ess_cmd1: %x, %x\n", cmd, val));
223 if (ess_dspwr(sc, cmd)) {
224 return ess_dspwr(sc, val & 0xff);
225 } else return 0;
226 }
227
228 static void
ess_setmixer(struct ess_info * sc,u_int port,u_int value)229 ess_setmixer(struct ess_info *sc, u_int port, u_int value)
230 {
231 DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);)
232 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
233 DELAY(10);
234 ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
235 DELAY(10);
236 }
237
238 static int
ess_getmixer(struct ess_info * sc,u_int port)239 ess_getmixer(struct ess_info *sc, u_int port)
240 {
241 int val;
242
243 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
244 DELAY(10);
245 val = ess_rd(sc, SB_MIX_DATA);
246 DELAY(10);
247
248 return val;
249 }
250
251 static int
ess_get_byte(struct ess_info * sc)252 ess_get_byte(struct ess_info *sc)
253 {
254 int i;
255
256 for (i = 1000; i > 0; i--) {
257 if (ess_rd(sc, 0xc) & 0x40)
258 return ess_rd(sc, DSP_READ);
259 else
260 DELAY(20);
261 }
262 return -1;
263 }
264
265 static int
ess_write(struct ess_info * sc,u_char reg,int val)266 ess_write(struct ess_info *sc, u_char reg, int val)
267 {
268 return ess_cmd1(sc, reg, val);
269 }
270
271 static int
ess_read(struct ess_info * sc,u_char reg)272 ess_read(struct ess_info *sc, u_char reg)
273 {
274 return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
275 }
276
277 static int
ess_reset_dsp(struct ess_info * sc)278 ess_reset_dsp(struct ess_info *sc)
279 {
280 DEB(printf("ess_reset_dsp\n"));
281 ess_wr(sc, SBDSP_RST, 3);
282 DELAY(100);
283 ess_wr(sc, SBDSP_RST, 0);
284 if (ess_get_byte(sc) != 0xAA) {
285 DEB(printf("ess_reset_dsp failed\n"));
286 /*
287 rman_get_start(d->io_base)));
288 */
289 return ENXIO; /* Sorry */
290 }
291 ess_cmd(sc, 0xc6);
292 return 0;
293 }
294
295 static void
ess_intr(void * arg)296 ess_intr(void *arg)
297 {
298 struct ess_info *sc = (struct ess_info *)arg;
299 int src, pirq = 0, rirq = 0;
300
301 ess_lock(sc);
302 src = 0;
303 if (ess_getmixer(sc, 0x7a) & 0x80)
304 src |= 2;
305 if (ess_rd(sc, 0x0c) & 0x01)
306 src |= 1;
307
308 if (src == 0) {
309 ess_unlock(sc);
310 return;
311 }
312
313 if (sc->duplex) {
314 pirq = (src & sc->pch.hwch)? 1 : 0;
315 rirq = (src & sc->rch.hwch)? 1 : 0;
316 } else {
317 if (sc->simplex_dir == PCMDIR_PLAY)
318 pirq = 1;
319 if (sc->simplex_dir == PCMDIR_REC)
320 rirq = 1;
321 if (!pirq && !rirq)
322 printf("solo: IRQ neither playback nor rec!\n");
323 }
324
325 DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq));
326
327 if (pirq) {
328 if (sc->pch.stopping) {
329 ess_dmatrigger(sc, sc->pch.hwch, 0);
330 sc->pch.stopping = 0;
331 if (sc->pch.hwch == 1)
332 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
333 else
334 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
335 }
336 ess_unlock(sc);
337 chn_intr(sc->pch.channel);
338 ess_lock(sc);
339 }
340
341 if (rirq) {
342 if (sc->rch.stopping) {
343 ess_dmatrigger(sc, sc->rch.hwch, 0);
344 sc->rch.stopping = 0;
345 /* XXX: will this stop audio2? */
346 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
347 }
348 ess_unlock(sc);
349 chn_intr(sc->rch.channel);
350 ess_lock(sc);
351 }
352
353 if (src & 2)
354 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
355 if (src & 1)
356 ess_rd(sc, DSP_DATA_AVAIL);
357
358 ess_unlock(sc);
359 }
360
361 /* utility functions for ESS */
362 static u_int8_t
ess_calcspeed8(int * spd)363 ess_calcspeed8(int *spd)
364 {
365 int speed = *spd;
366 u_int32_t t;
367
368 if (speed > 22000) {
369 t = (795500 + speed / 2) / speed;
370 speed = (795500 + t / 2) / t;
371 t = (256 - t) | 0x80;
372 } else {
373 t = (397700 + speed / 2) / speed;
374 speed = (397700 + t / 2) / t;
375 t = 128 - t;
376 }
377 *spd = speed;
378 return t & 0x000000ff;
379 }
380
381 static u_int8_t
ess_calcspeed9(int * spd)382 ess_calcspeed9(int *spd)
383 {
384 int speed, s0, s1, use0;
385 u_int8_t t0, t1;
386
387 /* rate = source / (256 - divisor) */
388 /* divisor = 256 - (source / rate) */
389 speed = *spd;
390 t0 = 128 - (793800 / speed);
391 s0 = 793800 / (128 - t0);
392
393 t1 = 128 - (768000 / speed);
394 s1 = 768000 / (128 - t1);
395 t1 |= 0x80;
396
397 use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
398
399 *spd = use0? s0 : s1;
400 return use0? t0 : t1;
401 }
402
403 static u_int8_t
ess_calcfilter(int spd)404 ess_calcfilter(int spd)
405 {
406 int cutoff;
407
408 /* cutoff = 7160000 / (256 - divisor) */
409 /* divisor = 256 - (7160000 / cutoff) */
410 cutoff = (spd * 9 * 82) / 20;
411 return (256 - (7160000 / cutoff));
412 }
413
414 static int
ess_setupch(struct ess_info * sc,int ch,int dir,int spd,u_int32_t fmt,int len)415 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
416 {
417 int play = (dir == PCMDIR_PLAY)? 1 : 0;
418 int b16 = (fmt & AFMT_16BIT)? 1 : 0;
419 int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0;
420 int unsign = (!(fmt & AFMT_SIGNED))? 1 : 0;
421 u_int8_t spdval, fmtval;
422
423 DEB(printf("ess_setupch\n"));
424 spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
425
426 sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ;
427
428 if (ch == 1) {
429 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
430 len = -len;
431 /* transfer length low */
432 ess_write(sc, 0xa4, len & 0x00ff);
433 /* transfer length high */
434 ess_write(sc, 0xa5, (len & 0xff00) >> 8);
435 /* autoinit, dma dir */
436 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
437 /* mono/stereo */
438 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
439 /* demand mode, 4 bytes/xfer */
440 ess_write(sc, 0xb9, 0x02);
441 /* sample rate */
442 ess_write(sc, 0xa1, spdval);
443 /* filter cutoff */
444 ess_write(sc, 0xa2, ess_calcfilter(spd));
445 /* setup dac/adc */
446 /*
447 if (play)
448 ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
449 */
450 /* mono, b16: signed, load signal */
451 /*
452 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
453 */
454 /* setup fifo */
455 ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) |
456 (b16? 0x04 : 0x00) |
457 (stereo? 0x08 : 0x40));
458 /* irq control */
459 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
460 /* drq control */
461 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
462 } else if (ch == 2) {
463 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
464 len >>= 1;
465 len = -len;
466 /* transfer length low */
467 ess_setmixer(sc, 0x74, len & 0x00ff);
468 /* transfer length high */
469 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
470 /* autoinit, 4 bytes/req */
471 ess_setmixer(sc, 0x78, 0x10);
472 fmtval = b16 | (stereo << 1) | ((!unsign) << 2);
473 /* enable irq, set format */
474 ess_setmixer(sc, 0x7a, 0x40 | fmtval);
475 if (sc->newspeed) {
476 /* sample rate */
477 ess_setmixer(sc, 0x70, spdval);
478 /* filter cutoff */
479 ess_setmixer(sc, 0x72, ess_calcfilter(spd));
480 }
481 }
482 return 0;
483 }
484 static int
ess_start(struct ess_chinfo * ch)485 ess_start(struct ess_chinfo *ch)
486 {
487 struct ess_info *sc = ch->parent;
488
489 DEB(printf("ess_start\n"););
490 ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
491 ch->stopping = 0;
492 if (ch->hwch == 1) {
493 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
494 if (ch->dir == PCMDIR_PLAY) {
495 #if 0
496 DELAY(100000); /* 100 ms */
497 #endif
498 ess_cmd(sc, 0xd1);
499 }
500 } else
501 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
502 return 0;
503 }
504
505 static int
ess_stop(struct ess_chinfo * ch)506 ess_stop(struct ess_chinfo *ch)
507 {
508 struct ess_info *sc = ch->parent;
509
510 DEB(printf("ess_stop\n"));
511 ch->stopping = 1;
512 if (ch->hwch == 1)
513 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
514 else
515 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
516 DEB(printf("done with stop\n"));
517 return 0;
518 }
519
520 /* -------------------------------------------------------------------- */
521 /* channel interface for ESS18xx */
522 static void *
esschan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)523 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
524 {
525 struct ess_info *sc = devinfo;
526 struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
527
528 DEB(printf("esschan_init\n"));
529 ch->parent = sc;
530 ch->channel = c;
531 ch->buffer = b;
532 ch->dir = dir;
533 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0)
534 return NULL;
535 ch->hwch = 1;
536 if ((dir == PCMDIR_PLAY) && (sc->duplex))
537 ch->hwch = 2;
538 return ch;
539 }
540
541 static int
esschan_setformat(kobj_t obj,void * data,u_int32_t format)542 esschan_setformat(kobj_t obj, void *data, u_int32_t format)
543 {
544 struct ess_chinfo *ch = data;
545
546 ch->fmt = format;
547 return 0;
548 }
549
550 static u_int32_t
esschan_setspeed(kobj_t obj,void * data,u_int32_t speed)551 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
552 {
553 struct ess_chinfo *ch = data;
554 struct ess_info *sc = ch->parent;
555
556 ch->spd = speed;
557 if (sc->newspeed)
558 ess_calcspeed9(&ch->spd);
559 else
560 ess_calcspeed8(&ch->spd);
561 return ch->spd;
562 }
563
564 static u_int32_t
esschan_setblocksize(kobj_t obj,void * data,u_int32_t blocksize)565 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
566 {
567 struct ess_chinfo *ch = data;
568
569 ch->blksz = blocksize;
570 return ch->blksz;
571 }
572
573 static int
esschan_trigger(kobj_t obj,void * data,int go)574 esschan_trigger(kobj_t obj, void *data, int go)
575 {
576 struct ess_chinfo *ch = data;
577 struct ess_info *sc = ch->parent;
578
579 if (!PCMTRIG_COMMON(go))
580 return 0;
581
582 DEB(printf("esschan_trigger: %d\n",go));
583
584 ess_lock(sc);
585 switch (go) {
586 case PCMTRIG_START:
587 ess_dmasetup(sc, ch->hwch, ch->buffer->buf_addr,
588 ch->buffer->bufsize, ch->dir);
589 ess_dmatrigger(sc, ch->hwch, 1);
590 ess_start(ch);
591 break;
592
593 case PCMTRIG_STOP:
594 case PCMTRIG_ABORT:
595 default:
596 ess_stop(ch);
597 break;
598 }
599 ess_unlock(sc);
600 return 0;
601 }
602
603 static u_int32_t
esschan_getptr(kobj_t obj,void * data)604 esschan_getptr(kobj_t obj, void *data)
605 {
606 struct ess_chinfo *ch = data;
607 struct ess_info *sc = ch->parent;
608 u_int32_t ret;
609
610 ess_lock(sc);
611 ret = ess_dmapos(sc, ch->hwch);
612 ess_unlock(sc);
613 return ret;
614 }
615
616 static struct pcmchan_caps *
esschan_getcaps(kobj_t obj,void * data)617 esschan_getcaps(kobj_t obj, void *data)
618 {
619 struct ess_chinfo *ch = data;
620
621 return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
622 }
623
624 static kobj_method_t esschan_methods[] = {
625 KOBJMETHOD(channel_init, esschan_init),
626 KOBJMETHOD(channel_setformat, esschan_setformat),
627 KOBJMETHOD(channel_setspeed, esschan_setspeed),
628 KOBJMETHOD(channel_setblocksize, esschan_setblocksize),
629 KOBJMETHOD(channel_trigger, esschan_trigger),
630 KOBJMETHOD(channel_getptr, esschan_getptr),
631 KOBJMETHOD(channel_getcaps, esschan_getcaps),
632 KOBJMETHOD_END
633 };
634 CHANNEL_DECLARE(esschan);
635
636 /************************************************************/
637
638 static int
essmix_init(struct snd_mixer * m)639 essmix_init(struct snd_mixer *m)
640 {
641 struct ess_info *sc = mix_getdevinfo(m);
642
643 mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
644 SOUND_MASK_IMIX);
645
646 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
647 SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
648 SOUND_MASK_LINE1);
649
650 ess_setmixer(sc, 0, 0); /* reset */
651
652 return 0;
653 }
654
655 static int
essmix_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)656 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
657 {
658 struct ess_info *sc = mix_getdevinfo(m);
659 int preg = 0, rreg = 0, l, r;
660
661 l = (left * 15) / 100;
662 r = (right * 15) / 100;
663 switch (dev) {
664 case SOUND_MIXER_SYNTH:
665 preg = 0x36;
666 rreg = 0x6b;
667 break;
668
669 case SOUND_MIXER_PCM:
670 preg = 0x14;
671 rreg = 0x7c;
672 break;
673
674 case SOUND_MIXER_LINE:
675 preg = 0x3e;
676 rreg = 0x6e;
677 break;
678
679 case SOUND_MIXER_MIC:
680 preg = 0x1a;
681 rreg = 0x68;
682 break;
683
684 case SOUND_MIXER_LINE1:
685 preg = 0x3a;
686 rreg = 0x6c;
687 break;
688
689 case SOUND_MIXER_CD:
690 preg = 0x38;
691 rreg = 0x6a;
692 break;
693
694 case SOUND_MIXER_VOLUME:
695 l = left? (left * 63) / 100 : 64;
696 r = right? (right * 63) / 100 : 64;
697 ess_setmixer(sc, 0x60, l);
698 ess_setmixer(sc, 0x62, r);
699 left = (l == 64)? 0 : (l * 100) / 63;
700 right = (r == 64)? 0 : (r * 100) / 63;
701 return left | (right << 8);
702 }
703
704 if (preg)
705 ess_setmixer(sc, preg, (l << 4) | r);
706 if (rreg)
707 ess_setmixer(sc, rreg, (l << 4) | r);
708
709 left = (l * 100) / 15;
710 right = (r * 100) / 15;
711
712 return left | (right << 8);
713 }
714
715 static u_int32_t
essmix_setrecsrc(struct snd_mixer * m,u_int32_t src)716 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
717 {
718 struct ess_info *sc = mix_getdevinfo(m);
719 u_char recdev;
720
721 switch (src) {
722 case SOUND_MASK_CD:
723 recdev = 0x02;
724 break;
725
726 case SOUND_MASK_LINE:
727 recdev = 0x06;
728 break;
729
730 case SOUND_MASK_IMIX:
731 recdev = 0x05;
732 break;
733
734 case SOUND_MASK_MIC:
735 default:
736 recdev = 0x00;
737 src = SOUND_MASK_MIC;
738 break;
739 }
740
741 ess_setmixer(sc, 0x1c, recdev);
742
743 return src;
744 }
745
746 static kobj_method_t solomixer_methods[] = {
747 KOBJMETHOD(mixer_init, essmix_init),
748 KOBJMETHOD(mixer_set, essmix_set),
749 KOBJMETHOD(mixer_setrecsrc, essmix_setrecsrc),
750 KOBJMETHOD_END
751 };
752 MIXER_DECLARE(solomixer);
753
754 /************************************************************/
755
756 static int
ess_dmasetup(struct ess_info * sc,int ch,u_int32_t base,u_int16_t cnt,int dir)757 ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir)
758 {
759 KASSERT(ch == 1 || ch == 2, ("bad ch"));
760 sc->dmasz[ch - 1] = cnt;
761 if (ch == 1) {
762 port_wr(sc->vc, 0x8, 0xc4, 1); /* command */
763 port_wr(sc->vc, 0xd, 0xff, 1); /* reset */
764 port_wr(sc->vc, 0xf, 0x01, 1); /* mask */
765 port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */
766 port_wr(sc->vc, 0x0, base, 4);
767 port_wr(sc->vc, 0x4, cnt - 1, 2);
768
769 } else if (ch == 2) {
770 port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */
771 port_wr(sc->io, 0x0, base, 4);
772 port_wr(sc->io, 0x4, cnt, 2);
773 }
774 return 0;
775 }
776
777 static int
ess_dmapos(struct ess_info * sc,int ch)778 ess_dmapos(struct ess_info *sc, int ch)
779 {
780 int p = 0, i = 0, j = 0;
781
782 KASSERT(ch == 1 || ch == 2, ("bad ch"));
783 if (ch == 1) {
784 /*
785 * During recording, this register is known to give back
786 * garbage if it's not quiescent while being read. That's
787 * why we spl, stop the DMA, and try over and over until
788 * adjacent reads are "close", in the right order and not
789 * bigger than is otherwise possible.
790 */
791 ess_dmatrigger(sc, ch, 0);
792 DELAY(20);
793 do {
794 DELAY(10);
795 if (j > 1)
796 printf("DMA count reg bogus: %04x & %04x\n",
797 i, p);
798 i = port_rd(sc->vc, 0x4, 2) + 1;
799 p = port_rd(sc->vc, 0x4, 2) + 1;
800 } while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000);
801 ess_dmatrigger(sc, ch, 1);
802 }
803 else if (ch == 2)
804 p = port_rd(sc->io, 0x4, 2);
805 return sc->dmasz[ch - 1] - p;
806 }
807
808 static int
ess_dmatrigger(struct ess_info * sc,int ch,int go)809 ess_dmatrigger(struct ess_info *sc, int ch, int go)
810 {
811 KASSERT(ch == 1 || ch == 2, ("bad ch"));
812 if (ch == 1)
813 port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */
814 else if (ch == 2)
815 port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */
816 return 0;
817 }
818
819 static void
ess_release_resources(struct ess_info * sc,device_t dev)820 ess_release_resources(struct ess_info *sc, device_t dev)
821 {
822 if (sc->irq) {
823 if (sc->ih)
824 bus_teardown_intr(dev, sc->irq, sc->ih);
825 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
826 sc->irq = NULL;
827 }
828 if (sc->io) {
829 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io);
830 sc->io = NULL;
831 }
832
833 if (sc->sb) {
834 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb);
835 sc->sb = NULL;
836 }
837
838 if (sc->vc) {
839 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc);
840 sc->vc = NULL;
841 }
842
843 if (sc->mpu) {
844 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu);
845 sc->mpu = NULL;
846 }
847
848 if (sc->gp) {
849 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp);
850 sc->gp = NULL;
851 }
852
853 if (sc->parent_dmat) {
854 bus_dma_tag_destroy(sc->parent_dmat);
855 sc->parent_dmat = 0;
856 }
857
858 if (sc->lock) {
859 snd_mtxfree(sc->lock);
860 sc->lock = NULL;
861 }
862
863 free(sc, M_DEVBUF);
864 }
865
866 static int
ess_alloc_resources(struct ess_info * sc,device_t dev)867 ess_alloc_resources(struct ess_info *sc, device_t dev)
868 {
869 int rid;
870
871 rid = PCIR_BAR(0);
872 sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
873
874 rid = PCIR_BAR(1);
875 sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
876
877 rid = PCIR_BAR(2);
878 sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
879
880 rid = PCIR_BAR(3);
881 sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
882
883 rid = PCIR_BAR(4);
884 sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
885
886 rid = 0;
887 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
888 RF_ACTIVE | RF_SHAREABLE);
889
890 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_solo softc");
891
892 return (sc->irq && sc->io && sc->sb && sc->vc &&
893 sc->mpu && sc->gp && sc->lock)? 0 : ENXIO;
894 }
895
896 static int
ess_probe(device_t dev)897 ess_probe(device_t dev)
898 {
899 char *s = NULL;
900 u_int32_t subdev;
901
902 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
903 switch (pci_get_devid(dev)) {
904 case 0x1969125d:
905 if (subdev == 0x8888125d)
906 s = "ESS Solo-1E";
907 else if (subdev == 0x1818125d)
908 s = "ESS Solo-1";
909 else
910 s = "ESS Solo-1 (unknown vendor)";
911 break;
912 }
913
914 if (s)
915 device_set_desc(dev, s);
916 return s ? BUS_PROBE_DEFAULT : ENXIO;
917 }
918
919 #define ESS_PCI_LEGACYCONTROL 0x40
920 #define ESS_PCI_CONFIG 0x50
921 #define ESS_PCI_DDMACONTROL 0x60
922
923 static int
ess_suspend(device_t dev)924 ess_suspend(device_t dev)
925 {
926 return 0;
927 }
928
929 static int
ess_resume(device_t dev)930 ess_resume(device_t dev)
931 {
932 uint16_t ddma;
933 struct ess_info *sc = pcm_getdevinfo(dev);
934
935 ess_lock(sc);
936 ddma = rman_get_start(sc->vc) | 1;
937 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
938 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
939 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
940
941 if (ess_reset_dsp(sc)) {
942 ess_unlock(sc);
943 goto no;
944 }
945 ess_unlock(sc);
946 if (mixer_reinit(dev))
947 goto no;
948 ess_lock(sc);
949 if (sc->newspeed)
950 ess_setmixer(sc, 0x71, 0x2a);
951
952 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
953 ess_unlock(sc);
954
955 return 0;
956 no:
957 return EIO;
958 }
959
960 static int
ess_attach(device_t dev)961 ess_attach(device_t dev)
962 {
963 struct ess_info *sc;
964 char status[SND_STATUSLEN];
965 u_int16_t ddma;
966
967 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
968 pci_enable_busmaster(dev);
969
970 if (ess_alloc_resources(sc, dev))
971 goto no;
972
973 sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536);
974
975 ddma = rman_get_start(sc->vc) | 1;
976 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
977 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
978 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
979
980 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
981 #ifdef ESS18XX_DUPLEX
982 sc->duplex = 1;
983 #else
984 sc->duplex = 0;
985 #endif
986
987 #ifdef ESS18XX_NEWSPEED
988 sc->newspeed = 1;
989 #else
990 sc->newspeed = 0;
991 #endif
992 if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ess_intr, sc, &sc->ih)) {
993 device_printf(dev, "unable to map interrupt\n");
994 goto no;
995 }
996
997 if (!sc->duplex)
998 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
999
1000 #if 0
1001 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/65536, /*boundary*/0,
1002 #endif
1003 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, /*boundary*/0,
1004 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
1005 /*highaddr*/BUS_SPACE_MAXADDR,
1006 /*filter*/NULL, /*filterarg*/NULL,
1007 /*maxsize*/sc->bufsz, /*nsegments*/1,
1008 /*maxsegz*/0x3ffff,
1009 /*flags*/0,
1010 /*lockfunc*/NULL, /*lockarg*/NULL,
1011 &sc->parent_dmat) != 0) {
1012 device_printf(dev, "unable to create dma tag\n");
1013 goto no;
1014 }
1015
1016 if (ess_reset_dsp(sc))
1017 goto no;
1018
1019 if (sc->newspeed)
1020 ess_setmixer(sc, 0x71, 0x2a);
1021
1022 if (mixer_init(dev, &solomixer_class, sc))
1023 goto no;
1024
1025 snprintf(status, SND_STATUSLEN, "port 0x%jx,0x%jx,0x%jx irq %jd on %s",
1026 rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc),
1027 rman_get_start(sc->irq),
1028 device_get_nameunit(device_get_parent(dev)));
1029
1030 pcm_init(dev, sc);
1031 pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
1032 pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
1033 if (pcm_register(dev, status))
1034 goto no;
1035
1036 return 0;
1037
1038 no:
1039 ess_release_resources(sc, dev);
1040 return ENXIO;
1041 }
1042
1043 static int
ess_detach(device_t dev)1044 ess_detach(device_t dev)
1045 {
1046 int r;
1047 struct ess_info *sc;
1048
1049 r = pcm_unregister(dev);
1050 if (r)
1051 return r;
1052
1053 sc = pcm_getdevinfo(dev);
1054 ess_release_resources(sc, dev);
1055 return 0;
1056 }
1057
1058 static device_method_t ess_methods[] = {
1059 /* Device interface */
1060 DEVMETHOD(device_probe, ess_probe),
1061 DEVMETHOD(device_attach, ess_attach),
1062 DEVMETHOD(device_detach, ess_detach),
1063 DEVMETHOD(device_resume, ess_resume),
1064 DEVMETHOD(device_suspend, ess_suspend),
1065 { 0, 0 }
1066 };
1067
1068 static driver_t ess_driver = {
1069 "pcm",
1070 ess_methods,
1071 PCM_SOFTC_SIZE,
1072 };
1073
1074 DRIVER_MODULE(snd_solo, pci, ess_driver, 0, 0);
1075 MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1076 MODULE_VERSION(snd_solo, 1);
1077