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