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