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