xref: /freebsd/sys/dev/sound/pci/solo.c (revision 306f91b60b9396b5a8333f5f5d0cb3d82a9e6609)
1fd1aaeccSCameron Grant /*
2fd1aaeccSCameron Grant  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3fd1aaeccSCameron Grant  *
4fd1aaeccSCameron Grant  * Redistribution and use in source and binary forms, with or without
5fd1aaeccSCameron Grant  * modification, are permitted provided that the following conditions
6fd1aaeccSCameron Grant  * are met:
7fd1aaeccSCameron Grant  * 1. Redistributions of source code must retain the above copyright
8fd1aaeccSCameron Grant  *    notice, this list of conditions and the following disclaimer.
9fd1aaeccSCameron Grant  * 2. Redistributions in binary form must reproduce the above copyright
10fd1aaeccSCameron Grant  *    notice, this list of conditions and the following disclaimer in the
11fd1aaeccSCameron Grant  *    documentation and/or other materials provided with the distribution.
12fd1aaeccSCameron Grant  *
13fd1aaeccSCameron Grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14fd1aaeccSCameron Grant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15fd1aaeccSCameron Grant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16fd1aaeccSCameron Grant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17fd1aaeccSCameron Grant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18fd1aaeccSCameron Grant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19fd1aaeccSCameron Grant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20fd1aaeccSCameron Grant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21fd1aaeccSCameron Grant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22fd1aaeccSCameron Grant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23fd1aaeccSCameron Grant  * SUCH DAMAGE.
24fd1aaeccSCameron Grant  *
25fd1aaeccSCameron Grant  * $FreeBSD$
26fd1aaeccSCameron Grant  */
27fd1aaeccSCameron Grant 
28fd1aaeccSCameron Grant #include <dev/sound/pcm/sound.h>
29fd1aaeccSCameron Grant 
30fd1aaeccSCameron Grant #include <pci/pcireg.h>
31fd1aaeccSCameron Grant #include <pci/pcivar.h>
32fd1aaeccSCameron Grant 
33fd1aaeccSCameron Grant #include  <dev/sound/isa/sb.h>
34fd1aaeccSCameron Grant #include  <dev/sound/chip.h>
35fd1aaeccSCameron Grant 
363ac1ca33SNick Sayer #define ESS_BUFFSIZE (16384)
37fd1aaeccSCameron Grant #define ABS(x) (((x) < 0)? -(x) : (x))
38fd1aaeccSCameron Grant 
393ac1ca33SNick Sayer /* if defined, playback always uses the 2nd channel and full duplex works */
40fd1aaeccSCameron Grant #undef ESS18XX_DUPLEX
41fd1aaeccSCameron Grant 
42fd1aaeccSCameron Grant /* more accurate clocks and split audio1/audio2 rates */
43fd1aaeccSCameron Grant #define ESS18XX_NEWSPEED
44fd1aaeccSCameron Grant 
45fd1aaeccSCameron Grant /* channel interface for ESS */
46fd1aaeccSCameron Grant static void *esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
47fd1aaeccSCameron Grant static int esschan_setdir(void *data, int dir);
48fd1aaeccSCameron Grant static int esschan_setformat(void *data, u_int32_t format);
49fd1aaeccSCameron Grant static int esschan_setspeed(void *data, u_int32_t speed);
50fd1aaeccSCameron Grant static int esschan_setblocksize(void *data, u_int32_t blocksize);
51fd1aaeccSCameron Grant static int esschan_trigger(void *data, int go);
52fd1aaeccSCameron Grant static int esschan_getptr(void *data);
53fd1aaeccSCameron Grant static pcmchan_caps *esschan_getcaps(void *data);
54fd1aaeccSCameron Grant 
55513693beSCameron Grant static u_int32_t ess_playfmt[] = {
56513693beSCameron Grant 	AFMT_U8,
57513693beSCameron Grant 	AFMT_STEREO | AFMT_U8,
58513693beSCameron Grant 	AFMT_S8,
59513693beSCameron Grant 	AFMT_STEREO | AFMT_S8,
60513693beSCameron Grant 	AFMT_S16_LE,
61513693beSCameron Grant 	AFMT_STEREO | AFMT_S16_LE,
62513693beSCameron Grant 	AFMT_U16_LE,
63513693beSCameron Grant 	AFMT_STEREO | AFMT_U16_LE,
64513693beSCameron Grant 	0
65fd1aaeccSCameron Grant };
66513693beSCameron Grant static pcmchan_caps ess_playcaps = {5000, 49000, ess_playfmt, 0};
67fd1aaeccSCameron Grant 
683ac1ca33SNick Sayer /*
693ac1ca33SNick Sayer  * Recording output is byte-swapped
703ac1ca33SNick Sayer  */
71513693beSCameron Grant static u_int32_t ess_recfmt[] = {
72513693beSCameron Grant 	AFMT_U8,
73513693beSCameron Grant 	AFMT_STEREO | AFMT_U8,
74513693beSCameron Grant 	AFMT_S8,
75513693beSCameron Grant 	AFMT_STEREO | AFMT_S8,
76513693beSCameron Grant 	AFMT_S16_BE,
77513693beSCameron Grant 	AFMT_STEREO | AFMT_S16_BE,
78513693beSCameron Grant 	AFMT_U16_BE,
79513693beSCameron Grant 	AFMT_STEREO | AFMT_U16_BE,
80513693beSCameron Grant 	0
81fd1aaeccSCameron Grant };
82513693beSCameron Grant static pcmchan_caps ess_reccaps = {5000, 49000, ess_recfmt, 0};
83fd1aaeccSCameron Grant 
84fd1aaeccSCameron Grant static pcm_channel ess_chantemplate = {
85fd1aaeccSCameron Grant 	esschan_init,
86306f91b6SCameron Grant 	NULL, 			/* setdir */
87fd1aaeccSCameron Grant 	esschan_setformat,
88fd1aaeccSCameron Grant 	esschan_setspeed,
89fd1aaeccSCameron Grant 	esschan_setblocksize,
90fd1aaeccSCameron Grant 	esschan_trigger,
91fd1aaeccSCameron Grant 	esschan_getptr,
92fd1aaeccSCameron Grant 	esschan_getcaps,
9333dbf14aSCameron Grant 	NULL, 			/* free */
9433dbf14aSCameron Grant 	NULL, 			/* nop1 */
9533dbf14aSCameron Grant 	NULL, 			/* nop2 */
9633dbf14aSCameron Grant 	NULL, 			/* nop3 */
9733dbf14aSCameron Grant 	NULL, 			/* nop4 */
9833dbf14aSCameron Grant 	NULL, 			/* nop5 */
9933dbf14aSCameron Grant 	NULL, 			/* nop6 */
10033dbf14aSCameron Grant 	NULL, 			/* nop7 */
101fd1aaeccSCameron Grant };
102fd1aaeccSCameron Grant 
103fd1aaeccSCameron Grant struct ess_info;
104fd1aaeccSCameron Grant 
105fd1aaeccSCameron Grant struct ess_chinfo {
106fd1aaeccSCameron Grant 	struct ess_info *parent;
107fd1aaeccSCameron Grant 	pcm_channel *channel;
108fd1aaeccSCameron Grant 	snd_dbuf *buffer;
109fd1aaeccSCameron Grant 	int dir, hwch, stopping;
110fd1aaeccSCameron Grant 	u_int32_t fmt, spd;
111fd1aaeccSCameron Grant };
112fd1aaeccSCameron Grant 
113fd1aaeccSCameron Grant struct ess_info {
114fd1aaeccSCameron Grant     	struct resource *io, *sb, *vc, *mpu, *gp;	/* I/O address for the board */
115fd1aaeccSCameron Grant     	struct resource *irq;
116306f91b6SCameron Grant 	void		*ih;
117fd1aaeccSCameron Grant     	bus_dma_tag_t parent_dmat;
118fd1aaeccSCameron Grant 
1193ac1ca33SNick Sayer     	int simplex_dir, type, duplex:1, newspeed:1, dmasz[2];
120fd1aaeccSCameron Grant     	struct ess_chinfo pch, rch;
121fd1aaeccSCameron Grant };
122fd1aaeccSCameron Grant 
123fd1aaeccSCameron Grant static int ess_rd(struct ess_info *sc, int reg);
124fd1aaeccSCameron Grant static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
125fd1aaeccSCameron Grant static int ess_dspready(struct ess_info *sc);
126fd1aaeccSCameron Grant static int ess_cmd(struct ess_info *sc, u_char val);
127fd1aaeccSCameron Grant static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
128fd1aaeccSCameron Grant static int ess_get_byte(struct ess_info *sc);
129fd1aaeccSCameron Grant static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
130fd1aaeccSCameron Grant static int ess_getmixer(struct ess_info *sc, u_int port);
131fd1aaeccSCameron Grant static int ess_reset_dsp(struct ess_info *sc);
132fd1aaeccSCameron Grant 
133fd1aaeccSCameron Grant static int ess_write(struct ess_info *sc, u_char reg, int val);
134fd1aaeccSCameron Grant static int ess_read(struct ess_info *sc, u_char reg);
135fd1aaeccSCameron Grant 
136fd1aaeccSCameron Grant static void ess_intr(void *arg);
137fd1aaeccSCameron Grant static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
138fd1aaeccSCameron Grant static int ess_start(struct ess_chinfo *ch);
139fd1aaeccSCameron Grant static int ess_stop(struct ess_chinfo *ch);
140fd1aaeccSCameron Grant 
141fd1aaeccSCameron Grant static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir);
142fd1aaeccSCameron Grant static int ess_dmapos(struct ess_info *sc, int ch);
143fd1aaeccSCameron Grant static int ess_dmatrigger(struct ess_info *sc, int ch, int go);
144fd1aaeccSCameron Grant 
145fd1aaeccSCameron Grant static int essmix_init(snd_mixer *m);
146fd1aaeccSCameron Grant static int essmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
147fd1aaeccSCameron Grant static int essmix_setrecsrc(snd_mixer *m, u_int32_t src);
148fd1aaeccSCameron Grant 
149fd1aaeccSCameron Grant static snd_mixer ess_mixer = {
150fd1aaeccSCameron Grant     	"ESS mixer",
151fd1aaeccSCameron Grant     	essmix_init,
15233dbf14aSCameron Grant 	NULL,
153fd1aaeccSCameron Grant     	essmix_set,
154fd1aaeccSCameron Grant     	essmix_setrecsrc,
155fd1aaeccSCameron Grant };
156fd1aaeccSCameron Grant 
157fd1aaeccSCameron Grant static devclass_t pcm_devclass;
158fd1aaeccSCameron Grant 
159fd1aaeccSCameron Grant /*
160fd1aaeccSCameron Grant  * Common code for the midi and pcm functions
161fd1aaeccSCameron Grant  *
162fd1aaeccSCameron Grant  * ess_cmd write a single byte to the CMD port.
163fd1aaeccSCameron Grant  * ess_cmd1 write a CMD + 1 byte arg
164fd1aaeccSCameron Grant  * ess_cmd2 write a CMD + 2 byte arg
165fd1aaeccSCameron Grant  * ess_get_byte returns a single byte from the DSP data port
166fd1aaeccSCameron Grant  *
167fd1aaeccSCameron Grant  * ess_write is actually ess_cmd1
168fd1aaeccSCameron Grant  * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
169fd1aaeccSCameron Grant  */
170fd1aaeccSCameron Grant 
171fd1aaeccSCameron Grant static int
172fd1aaeccSCameron Grant port_rd(struct resource *port, int regno, int size)
173fd1aaeccSCameron Grant {
174fd1aaeccSCameron Grant 	bus_space_tag_t st = rman_get_bustag(port);
175fd1aaeccSCameron Grant 	bus_space_handle_t sh = rman_get_bushandle(port);
176fd1aaeccSCameron Grant 
177fd1aaeccSCameron Grant 	switch (size) {
178fd1aaeccSCameron Grant 	case 1:
179fd1aaeccSCameron Grant 		return bus_space_read_1(st, sh, regno);
180fd1aaeccSCameron Grant 	case 2:
181fd1aaeccSCameron Grant 		return bus_space_read_2(st, sh, regno);
182fd1aaeccSCameron Grant 	case 4:
183fd1aaeccSCameron Grant 		return bus_space_read_4(st, sh, regno);
184fd1aaeccSCameron Grant 	default:
185fd1aaeccSCameron Grant 		return 0xffffffff;
186fd1aaeccSCameron Grant 	}
187fd1aaeccSCameron Grant }
188fd1aaeccSCameron Grant 
189fd1aaeccSCameron Grant static void
190fd1aaeccSCameron Grant port_wr(struct resource *port, int regno, u_int32_t data, int size)
191fd1aaeccSCameron Grant {
192fd1aaeccSCameron Grant 	bus_space_tag_t st = rman_get_bustag(port);
193fd1aaeccSCameron Grant 	bus_space_handle_t sh = rman_get_bushandle(port);
194fd1aaeccSCameron Grant 
195fd1aaeccSCameron Grant 	switch (size) {
196fd1aaeccSCameron Grant 	case 1:
197fd1aaeccSCameron Grant 		bus_space_write_1(st, sh, regno, data);
198fd1aaeccSCameron Grant 		break;
199fd1aaeccSCameron Grant 	case 2:
200fd1aaeccSCameron Grant 		bus_space_write_2(st, sh, regno, data);
201fd1aaeccSCameron Grant 		break;
202fd1aaeccSCameron Grant 	case 4:
203fd1aaeccSCameron Grant 		bus_space_write_4(st, sh, regno, data);
204fd1aaeccSCameron Grant 		break;
205fd1aaeccSCameron Grant 	}
206fd1aaeccSCameron Grant }
207fd1aaeccSCameron Grant 
208fd1aaeccSCameron Grant static int
209fd1aaeccSCameron Grant ess_rd(struct ess_info *sc, int reg)
210fd1aaeccSCameron Grant {
211fd1aaeccSCameron Grant 	return port_rd(sc->sb, reg, 1);
212fd1aaeccSCameron Grant }
213fd1aaeccSCameron Grant 
214fd1aaeccSCameron Grant static void
215fd1aaeccSCameron Grant ess_wr(struct ess_info *sc, int reg, u_int8_t val)
216fd1aaeccSCameron Grant {
217fd1aaeccSCameron Grant 	port_wr(sc->sb, reg, val, 1);
218fd1aaeccSCameron Grant }
219fd1aaeccSCameron Grant 
220fd1aaeccSCameron Grant static int
221fd1aaeccSCameron Grant ess_dspready(struct ess_info *sc)
222fd1aaeccSCameron Grant {
223fd1aaeccSCameron Grant 	return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
224fd1aaeccSCameron Grant }
225fd1aaeccSCameron Grant 
226fd1aaeccSCameron Grant static int
227fd1aaeccSCameron Grant ess_dspwr(struct ess_info *sc, u_char val)
228fd1aaeccSCameron Grant {
229fd1aaeccSCameron Grant     	int  i;
230fd1aaeccSCameron Grant 
231fd1aaeccSCameron Grant     	for (i = 0; i < 1000; i++) {
232fd1aaeccSCameron Grant 		if (ess_dspready(sc)) {
233fd1aaeccSCameron Grant 	    		ess_wr(sc, SBDSP_CMD, val);
234fd1aaeccSCameron Grant 	    		return 1;
235fd1aaeccSCameron Grant 		}
236fd1aaeccSCameron Grant 		if (i > 10) DELAY((i > 100)? 1000 : 10);
237fd1aaeccSCameron Grant     	}
238fd1aaeccSCameron Grant     	printf("ess_dspwr(0x%02x) timed out.\n", val);
239fd1aaeccSCameron Grant     	return 0;
240fd1aaeccSCameron Grant }
241fd1aaeccSCameron Grant 
242fd1aaeccSCameron Grant static int
243fd1aaeccSCameron Grant ess_cmd(struct ess_info *sc, u_char val)
244fd1aaeccSCameron Grant {
245a7e11506SNick Sayer 	DEB(printf("ess_cmd: %x\n", val));
246fd1aaeccSCameron Grant     	return ess_dspwr(sc, val);
247fd1aaeccSCameron Grant }
248fd1aaeccSCameron Grant 
249fd1aaeccSCameron Grant static int
250fd1aaeccSCameron Grant ess_cmd1(struct ess_info *sc, u_char cmd, int val)
251fd1aaeccSCameron Grant {
252a7e11506SNick Sayer     	DEB(printf("ess_cmd1: %x, %x\n", cmd, val));
253fd1aaeccSCameron Grant     	if (ess_dspwr(sc, cmd)) {
254fd1aaeccSCameron Grant 		return ess_dspwr(sc, val & 0xff);
255fd1aaeccSCameron Grant     	} else return 0;
256fd1aaeccSCameron Grant }
257fd1aaeccSCameron Grant 
258fd1aaeccSCameron Grant static void
259fd1aaeccSCameron Grant ess_setmixer(struct ess_info *sc, u_int port, u_int value)
260fd1aaeccSCameron Grant {
261fd1aaeccSCameron Grant     	u_long   flags;
262fd1aaeccSCameron Grant 
263fd1aaeccSCameron Grant 	DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);)
264fd1aaeccSCameron Grant     	flags = spltty();
265fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
266fd1aaeccSCameron Grant     	DELAY(10);
267fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
268fd1aaeccSCameron Grant     	DELAY(10);
269fd1aaeccSCameron Grant     	splx(flags);
270fd1aaeccSCameron Grant }
271fd1aaeccSCameron Grant 
272fd1aaeccSCameron Grant static int
273fd1aaeccSCameron Grant ess_getmixer(struct ess_info *sc, u_int port)
274fd1aaeccSCameron Grant {
275fd1aaeccSCameron Grant     	int val;
276fd1aaeccSCameron Grant     	u_long flags;
277fd1aaeccSCameron Grant 
278fd1aaeccSCameron Grant     	flags = spltty();
279fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
280fd1aaeccSCameron Grant     	DELAY(10);
281fd1aaeccSCameron Grant     	val = ess_rd(sc, SB_MIX_DATA);
282fd1aaeccSCameron Grant     	DELAY(10);
283fd1aaeccSCameron Grant     	splx(flags);
284fd1aaeccSCameron Grant 
285fd1aaeccSCameron Grant     	return val;
286fd1aaeccSCameron Grant }
287fd1aaeccSCameron Grant 
288fd1aaeccSCameron Grant static int
289fd1aaeccSCameron Grant ess_get_byte(struct ess_info *sc)
290fd1aaeccSCameron Grant {
291fd1aaeccSCameron Grant     	int i;
292fd1aaeccSCameron Grant 
293fd1aaeccSCameron Grant     	for (i = 1000; i > 0; i--) {
2940edeb3dcSNick Sayer 		if (ess_rd(sc, 0xc) & 0x40)
295fd1aaeccSCameron Grant 			return ess_rd(sc, DSP_READ);
296fd1aaeccSCameron Grant 		else
297fd1aaeccSCameron Grant 			DELAY(20);
298fd1aaeccSCameron Grant     	}
299fd1aaeccSCameron Grant     	return -1;
300fd1aaeccSCameron Grant }
301fd1aaeccSCameron Grant 
302fd1aaeccSCameron Grant static int
303fd1aaeccSCameron Grant ess_write(struct ess_info *sc, u_char reg, int val)
304fd1aaeccSCameron Grant {
305fd1aaeccSCameron Grant     	return ess_cmd1(sc, reg, val);
306fd1aaeccSCameron Grant }
307fd1aaeccSCameron Grant 
308fd1aaeccSCameron Grant static int
309fd1aaeccSCameron Grant ess_read(struct ess_info *sc, u_char reg)
310fd1aaeccSCameron Grant {
311fd1aaeccSCameron Grant     	return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
312fd1aaeccSCameron Grant }
313fd1aaeccSCameron Grant 
314fd1aaeccSCameron Grant static int
315fd1aaeccSCameron Grant ess_reset_dsp(struct ess_info *sc)
316fd1aaeccSCameron Grant {
317a7e11506SNick Sayer 	DEB(printf("ess_reset_dsp\n"));
318fd1aaeccSCameron Grant     	ess_wr(sc, SBDSP_RST, 3);
319fd1aaeccSCameron Grant     	DELAY(100);
320fd1aaeccSCameron Grant     	ess_wr(sc, SBDSP_RST, 0);
321fd1aaeccSCameron Grant     	if (ess_get_byte(sc) != 0xAA) {
322a7e11506SNick Sayer         	DEB(printf("ess_reset_dsp failed\n"));
323a7e11506SNick Sayer /*
324fd1aaeccSCameron Grant 			   rman_get_start(d->io_base)));
325a7e11506SNick Sayer */
326fd1aaeccSCameron Grant 		return ENXIO;	/* Sorry */
327fd1aaeccSCameron Grant     	}
328fd1aaeccSCameron Grant     	ess_cmd(sc, 0xc6);
329fd1aaeccSCameron Grant     	return 0;
330fd1aaeccSCameron Grant }
331fd1aaeccSCameron Grant 
332fd1aaeccSCameron Grant static void
333fd1aaeccSCameron Grant ess_intr(void *arg)
334fd1aaeccSCameron Grant {
335fd1aaeccSCameron Grant     	struct ess_info *sc = (struct ess_info *)arg;
3363ac1ca33SNick Sayer 	int src, pirq = 0, rirq = 0;
337fd1aaeccSCameron Grant 
338fd1aaeccSCameron Grant 	src = 0;
339fd1aaeccSCameron Grant 	if (ess_getmixer(sc, 0x7a) & 0x80)
340fd1aaeccSCameron Grant 		src |= 2;
341fd1aaeccSCameron Grant 	if (ess_rd(sc, 0x0c) & 0x01)
342fd1aaeccSCameron Grant 		src |= 1;
343fd1aaeccSCameron Grant 
3444e77c048SCameron Grant 	if (src == 0)
3454e77c048SCameron Grant 		return;
3464e77c048SCameron Grant 
3473ac1ca33SNick Sayer 	if (sc->duplex) {
348fd1aaeccSCameron Grant 		pirq = (src & sc->pch.hwch)? 1 : 0;
349fd1aaeccSCameron Grant 		rirq = (src & sc->rch.hwch)? 1 : 0;
3503ac1ca33SNick Sayer 	} else {
3513ac1ca33SNick Sayer 		if (sc->simplex_dir == PCMDIR_PLAY)
3523ac1ca33SNick Sayer 			pirq = 1;
3533ac1ca33SNick Sayer 		if (sc->simplex_dir == PCMDIR_REC)
3543ac1ca33SNick Sayer 			rirq = 1;
3553ac1ca33SNick Sayer 		if (!pirq && !rirq)
3563ac1ca33SNick Sayer 			printf("solo: IRQ neither playback nor rec!\n");
3573ac1ca33SNick Sayer 	}
358fd1aaeccSCameron Grant 
359a7e11506SNick Sayer 	DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq));
360a7e11506SNick Sayer 
361fd1aaeccSCameron Grant 	if (pirq) {
362fd1aaeccSCameron Grant 		if (sc->pch.stopping) {
363fd1aaeccSCameron Grant 			ess_dmatrigger(sc, sc->pch.hwch, 0);
364fd1aaeccSCameron Grant 			sc->pch.stopping = 0;
365fd1aaeccSCameron Grant 			if (sc->pch.hwch == 1)
366fd1aaeccSCameron Grant 				ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
367fd1aaeccSCameron Grant 			else
368fd1aaeccSCameron Grant 				ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
369fd1aaeccSCameron Grant 		}
370fd1aaeccSCameron Grant 		chn_intr(sc->pch.channel);
371fd1aaeccSCameron Grant 	}
372fd1aaeccSCameron Grant 
373fd1aaeccSCameron Grant 	if (rirq) {
374fd1aaeccSCameron Grant 		if (sc->rch.stopping) {
375fd1aaeccSCameron Grant 			ess_dmatrigger(sc, sc->rch.hwch, 0);
376fd1aaeccSCameron Grant 			sc->rch.stopping = 0;
377fd1aaeccSCameron Grant 			/* XXX: will this stop audio2? */
378fd1aaeccSCameron Grant 			ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
379fd1aaeccSCameron Grant 		}
380fd1aaeccSCameron Grant 		chn_intr(sc->rch.channel);
381fd1aaeccSCameron Grant 	}
382fd1aaeccSCameron Grant 
383fd1aaeccSCameron Grant 	if (src & 2)
384fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
385fd1aaeccSCameron Grant 	if (src & 1)
386fd1aaeccSCameron Grant     		ess_rd(sc, DSP_DATA_AVAIL);
387fd1aaeccSCameron Grant }
388fd1aaeccSCameron Grant 
389fd1aaeccSCameron Grant /* utility functions for ESS */
390fd1aaeccSCameron Grant static u_int8_t
391fd1aaeccSCameron Grant ess_calcspeed8(int *spd)
392fd1aaeccSCameron Grant {
393fd1aaeccSCameron Grant 	int speed = *spd;
394fd1aaeccSCameron Grant 	u_int32_t t;
395fd1aaeccSCameron Grant 
396fd1aaeccSCameron Grant 	if (speed > 22000) {
397fd1aaeccSCameron Grant 		t = (795500 + speed / 2) / speed;
398fd1aaeccSCameron Grant 		speed = (795500 + t / 2) / t;
399fd1aaeccSCameron Grant 		t = (256 - t) | 0x80;
400fd1aaeccSCameron Grant 	} else {
401fd1aaeccSCameron Grant 		t = (397700 + speed / 2) / speed;
402fd1aaeccSCameron Grant 		speed = (397700 + t / 2) / t;
403fd1aaeccSCameron Grant 		t = 128 - t;
404fd1aaeccSCameron Grant 	}
405fd1aaeccSCameron Grant 	*spd = speed;
406fd1aaeccSCameron Grant 	return t & 0x000000ff;
407fd1aaeccSCameron Grant }
408fd1aaeccSCameron Grant 
409fd1aaeccSCameron Grant static u_int8_t
410fd1aaeccSCameron Grant ess_calcspeed9(int *spd)
411fd1aaeccSCameron Grant {
412fd1aaeccSCameron Grant 	int speed, s0, s1, use0;
413fd1aaeccSCameron Grant 	u_int8_t t0, t1;
414fd1aaeccSCameron Grant 
415fd1aaeccSCameron Grant 	/* rate = source / (256 - divisor) */
416fd1aaeccSCameron Grant 	/* divisor = 256 - (source / rate) */
417fd1aaeccSCameron Grant 	speed = *spd;
418fd1aaeccSCameron Grant 	t0 = 128 - (793800 / speed);
419fd1aaeccSCameron Grant 	s0 = 793800 / (128 - t0);
420fd1aaeccSCameron Grant 
421fd1aaeccSCameron Grant 	t1 = 128 - (768000 / speed);
422fd1aaeccSCameron Grant 	s1 = 768000 / (128 - t1);
423fd1aaeccSCameron Grant 	t1 |= 0x80;
424fd1aaeccSCameron Grant 
425fd1aaeccSCameron Grant 	use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
426fd1aaeccSCameron Grant 
427fd1aaeccSCameron Grant 	*spd = use0? s0 : s1;
428fd1aaeccSCameron Grant 	return use0? t0 : t1;
429fd1aaeccSCameron Grant }
430fd1aaeccSCameron Grant 
431fd1aaeccSCameron Grant static u_int8_t
432fd1aaeccSCameron Grant ess_calcfilter(int spd)
433fd1aaeccSCameron Grant {
434fd1aaeccSCameron Grant 	int cutoff;
435fd1aaeccSCameron Grant 
436fd1aaeccSCameron Grant 	/* cutoff = 7160000 / (256 - divisor) */
437fd1aaeccSCameron Grant 	/* divisor = 256 - (7160000 / cutoff) */
438fd1aaeccSCameron Grant 	cutoff = (spd * 9 * 82) / 20;
439fd1aaeccSCameron Grant 	return (256 - (7160000 / cutoff));
440fd1aaeccSCameron Grant }
441fd1aaeccSCameron Grant 
442fd1aaeccSCameron Grant static int
443fd1aaeccSCameron Grant ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
444fd1aaeccSCameron Grant {
445fd1aaeccSCameron Grant 	int play = (dir == PCMDIR_PLAY)? 1 : 0;
446fd1aaeccSCameron Grant 	int b16 = (fmt & AFMT_16BIT)? 1 : 0;
447fd1aaeccSCameron Grant 	int stereo = (fmt & AFMT_STEREO)? 1 : 0;
4483ac1ca33SNick Sayer 	int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE || fmt == AFMT_U16_BE)? 1 : 0;
449fd1aaeccSCameron Grant 	u_int8_t spdval, fmtval;
450fd1aaeccSCameron Grant 
451a7e11506SNick Sayer 	DEB(printf("ess_setupch\n"));
452fd1aaeccSCameron Grant 	spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
453fd1aaeccSCameron Grant 
4543ac1ca33SNick Sayer 	sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ;
4553ac1ca33SNick Sayer 
456fd1aaeccSCameron Grant 	if (ch == 1) {
457fd1aaeccSCameron Grant 		KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
458fd1aaeccSCameron Grant 		len = -len;
459fd1aaeccSCameron Grant 		/* transfer length low */
460fd1aaeccSCameron Grant 		ess_write(sc, 0xa4, len & 0x00ff);
461fd1aaeccSCameron Grant 		/* transfer length high */
462fd1aaeccSCameron Grant 		ess_write(sc, 0xa5, (len & 0xff00) >> 8);
463fd1aaeccSCameron Grant 		/* autoinit, dma dir */
464a7e11506SNick Sayer 		ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
465fd1aaeccSCameron Grant 		/* mono/stereo */
466fd1aaeccSCameron Grant 		ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
467fd1aaeccSCameron Grant 		/* demand mode, 4 bytes/xfer */
468fd1aaeccSCameron Grant 		ess_write(sc, 0xb9, 0x02);
469fd1aaeccSCameron Grant 		/* sample rate */
470fd1aaeccSCameron Grant         	ess_write(sc, 0xa1, spdval);
471fd1aaeccSCameron Grant 		/* filter cutoff */
472fd1aaeccSCameron Grant 		ess_write(sc, 0xa2, ess_calcfilter(spd));
473fd1aaeccSCameron Grant 		/* setup dac/adc */
474fd1aaeccSCameron Grant 		/*
475fd1aaeccSCameron Grant 		if (play)
476fd1aaeccSCameron Grant 			ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
477fd1aaeccSCameron Grant 		*/
478fd1aaeccSCameron Grant 		/* mono, b16: signed, load signal */
479fd1aaeccSCameron Grant 		/*
480fd1aaeccSCameron Grant 		ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
481fd1aaeccSCameron Grant 		*/
482fd1aaeccSCameron Grant 		/* setup fifo */
483a7e11506SNick Sayer 		ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) |
484fd1aaeccSCameron Grant 					   (b16? 0x04 : 0x00) |
485fd1aaeccSCameron Grant 					   (stereo? 0x08 : 0x40));
486fd1aaeccSCameron Grant 		/* irq control */
487fd1aaeccSCameron Grant 		ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
488fd1aaeccSCameron Grant 		/* drq control */
489fd1aaeccSCameron Grant 		ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
490fd1aaeccSCameron Grant 	} else if (ch == 2) {
491fd1aaeccSCameron Grant 		KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
492fd1aaeccSCameron Grant 		len >>= 1;
493fd1aaeccSCameron Grant 		len = -len;
494fd1aaeccSCameron Grant 		/* transfer length low */
495fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x74, len & 0x00ff);
496fd1aaeccSCameron Grant 		/* transfer length high */
497fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
498fd1aaeccSCameron Grant 		/* autoinit, 4 bytes/req */
499fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, 0x10);
50080a8e065SNick Sayer 		fmtval = b16 | (stereo << 1) | ((!unsign) << 2);
501fd1aaeccSCameron Grant 		/* enable irq, set format */
502fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x7a, 0x40 | fmtval);
503fd1aaeccSCameron Grant 		if (sc->newspeed) {
504fd1aaeccSCameron Grant 			/* sample rate */
505fd1aaeccSCameron Grant 			ess_setmixer(sc, 0x70, spdval);
506fd1aaeccSCameron Grant 			/* filter cutoff */
507fd1aaeccSCameron Grant 			ess_setmixer(sc, 0x72, ess_calcfilter(spd));
508fd1aaeccSCameron Grant 		}
509fd1aaeccSCameron Grant 
510fd1aaeccSCameron Grant 	}
511fd1aaeccSCameron Grant 	return 0;
512fd1aaeccSCameron Grant }
513fd1aaeccSCameron Grant static int
514fd1aaeccSCameron Grant ess_start(struct ess_chinfo *ch)
515fd1aaeccSCameron Grant {
516fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
517fd1aaeccSCameron Grant 
518a7e11506SNick Sayer 	DEB(printf("ess_start\n"););
519fd1aaeccSCameron Grant 	ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->buffer->dl);
520fd1aaeccSCameron Grant 	ch->stopping = 0;
52119a0702eSNick Sayer 	if (ch->hwch == 1) {
522fd1aaeccSCameron Grant 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
52319a0702eSNick Sayer 		if (ch->dir == PCMDIR_PLAY) {
5240edeb3dcSNick Sayer #if 0
52519a0702eSNick Sayer 			DELAY(100000); /* 100 ms */
5260edeb3dcSNick Sayer #endif
52719a0702eSNick Sayer 			ess_cmd(sc, 0xd1);
52819a0702eSNick Sayer 		}
52919a0702eSNick Sayer 	} else
530fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
531fd1aaeccSCameron Grant 	return 0;
532fd1aaeccSCameron Grant }
533fd1aaeccSCameron Grant 
534fd1aaeccSCameron Grant static int
535fd1aaeccSCameron Grant ess_stop(struct ess_chinfo *ch)
536fd1aaeccSCameron Grant {
537fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
538fd1aaeccSCameron Grant 
539a7e11506SNick Sayer 	DEB(printf("ess_stop\n"));
540fd1aaeccSCameron Grant 	ch->stopping = 1;
541fd1aaeccSCameron Grant 	if (ch->hwch == 1)
542fd1aaeccSCameron Grant 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
543fd1aaeccSCameron Grant 	else
544fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
545a7e11506SNick Sayer 	DEB(printf("done with stop\n"));
546fd1aaeccSCameron Grant 	return 0;
547fd1aaeccSCameron Grant }
548fd1aaeccSCameron Grant 
549fd1aaeccSCameron Grant /* channel interface for ESS18xx */
550fd1aaeccSCameron Grant static void *
551fd1aaeccSCameron Grant esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
552fd1aaeccSCameron Grant {
553fd1aaeccSCameron Grant 	struct ess_info *sc = devinfo;
554fd1aaeccSCameron Grant 	struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
555fd1aaeccSCameron Grant 
556a7e11506SNick Sayer 	DEB(printf("esschan_init\n"));
557fd1aaeccSCameron Grant 	ch->parent = sc;
558fd1aaeccSCameron Grant 	ch->channel = c;
559fd1aaeccSCameron Grant 	ch->buffer = b;
560fd1aaeccSCameron Grant 	ch->buffer->bufsize = ESS_BUFFSIZE;
561306f91b6SCameron Grant 	ch->dir = dir;
562fd1aaeccSCameron Grant 	if (chn_allocbuf(ch->buffer, sc->parent_dmat) == -1)
563fd1aaeccSCameron Grant 		return NULL;
564fd1aaeccSCameron Grant 	ch->hwch = 1;
565fd1aaeccSCameron Grant 	if ((dir == PCMDIR_PLAY) && (sc->duplex))
566fd1aaeccSCameron Grant 		ch->hwch = 2;
567fd1aaeccSCameron Grant 	return ch;
568fd1aaeccSCameron Grant }
569fd1aaeccSCameron Grant 
570fd1aaeccSCameron Grant static int
571fd1aaeccSCameron Grant esschan_setformat(void *data, u_int32_t format)
572fd1aaeccSCameron Grant {
573fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
574fd1aaeccSCameron Grant 
575fd1aaeccSCameron Grant 	ch->fmt = format;
576fd1aaeccSCameron Grant 	return 0;
577fd1aaeccSCameron Grant }
578fd1aaeccSCameron Grant 
579fd1aaeccSCameron Grant static int
580fd1aaeccSCameron Grant esschan_setspeed(void *data, u_int32_t speed)
581fd1aaeccSCameron Grant {
582fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
583fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
584fd1aaeccSCameron Grant 
585fd1aaeccSCameron Grant 	ch->spd = speed;
586fd1aaeccSCameron Grant 	if (sc->newspeed)
587fd1aaeccSCameron Grant 		ess_calcspeed9(&ch->spd);
588fd1aaeccSCameron Grant 	else
589fd1aaeccSCameron Grant 		ess_calcspeed8(&ch->spd);
590fd1aaeccSCameron Grant 	return ch->spd;
591fd1aaeccSCameron Grant }
592fd1aaeccSCameron Grant 
593fd1aaeccSCameron Grant static int
594fd1aaeccSCameron Grant esschan_setblocksize(void *data, u_int32_t blocksize)
595fd1aaeccSCameron Grant {
596fd1aaeccSCameron Grant 	return blocksize;
597fd1aaeccSCameron Grant }
598fd1aaeccSCameron Grant 
599fd1aaeccSCameron Grant static int
600fd1aaeccSCameron Grant esschan_trigger(void *data, int go)
601fd1aaeccSCameron Grant {
602fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
603fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
604fd1aaeccSCameron Grant 
605a7e11506SNick Sayer 	DEB(printf("esschan_trigger: %d\n",go));
606fd1aaeccSCameron Grant 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
607fd1aaeccSCameron Grant 		return 0;
608fd1aaeccSCameron Grant 
609fd1aaeccSCameron Grant 	switch (go) {
610fd1aaeccSCameron Grant 	case PCMTRIG_START:
611fd1aaeccSCameron Grant 		ess_dmasetup(sc, ch->hwch, vtophys(ch->buffer->buf), ch->buffer->bufsize, ch->dir);
612fd1aaeccSCameron Grant 		ess_dmatrigger(sc, ch->hwch, 1);
613fd1aaeccSCameron Grant 		ess_start(ch);
614fd1aaeccSCameron Grant 		break;
615fd1aaeccSCameron Grant 
616fd1aaeccSCameron Grant 	case PCMTRIG_STOP:
617fd1aaeccSCameron Grant 	case PCMTRIG_ABORT:
618fd1aaeccSCameron Grant 	default:
619fd1aaeccSCameron Grant 		ess_stop(ch);
620fd1aaeccSCameron Grant 		break;
621fd1aaeccSCameron Grant 	}
622fd1aaeccSCameron Grant 	return 0;
623fd1aaeccSCameron Grant }
624fd1aaeccSCameron Grant 
625fd1aaeccSCameron Grant static int
626fd1aaeccSCameron Grant esschan_getptr(void *data)
627fd1aaeccSCameron Grant {
628fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
629fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
630fd1aaeccSCameron Grant 
631fd1aaeccSCameron Grant 	return ess_dmapos(sc, ch->hwch);
632fd1aaeccSCameron Grant }
633fd1aaeccSCameron Grant 
634fd1aaeccSCameron Grant static pcmchan_caps *
635fd1aaeccSCameron Grant esschan_getcaps(void *data)
636fd1aaeccSCameron Grant {
637fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
638fd1aaeccSCameron Grant 
639fd1aaeccSCameron Grant 	return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
640fd1aaeccSCameron Grant }
641fd1aaeccSCameron Grant 
642fd1aaeccSCameron Grant /************************************************************/
643fd1aaeccSCameron Grant 
644fd1aaeccSCameron Grant static int
645fd1aaeccSCameron Grant essmix_init(snd_mixer *m)
646fd1aaeccSCameron Grant {
647fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
648fd1aaeccSCameron Grant 
649fd1aaeccSCameron Grant 	mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
650fd1aaeccSCameron Grant 			  SOUND_MASK_IMIX);
651fd1aaeccSCameron Grant 
652fd1aaeccSCameron Grant 	mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
653fd1aaeccSCameron Grant 		       SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
654fd1aaeccSCameron Grant 		       SOUND_MASK_LINE1);
655fd1aaeccSCameron Grant 
656fd1aaeccSCameron Grant 	ess_setmixer(sc, 0, 0); /* reset */
657fd1aaeccSCameron Grant 
658fd1aaeccSCameron Grant 	return 0;
659fd1aaeccSCameron Grant }
660fd1aaeccSCameron Grant 
661fd1aaeccSCameron Grant static int
662fd1aaeccSCameron Grant essmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
663fd1aaeccSCameron Grant {
664fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
665fd1aaeccSCameron Grant     	int preg = 0, rreg = 0, l, r;
666fd1aaeccSCameron Grant 
667fd1aaeccSCameron Grant 	l = (left * 15) / 100;
668fd1aaeccSCameron Grant 	r = (right * 15) / 100;
669fd1aaeccSCameron Grant 	switch (dev) {
670fd1aaeccSCameron Grant 	case SOUND_MIXER_SYNTH:
671fd1aaeccSCameron Grant 		preg = 0x36;
672fd1aaeccSCameron Grant 		rreg = 0x6b;
673fd1aaeccSCameron Grant 		break;
674fd1aaeccSCameron Grant 
675fd1aaeccSCameron Grant 	case SOUND_MIXER_PCM:
676fd1aaeccSCameron Grant 		preg = 0x14;
677fd1aaeccSCameron Grant 		rreg = 0x7c;
678fd1aaeccSCameron Grant 		break;
679fd1aaeccSCameron Grant 
680fd1aaeccSCameron Grant 	case SOUND_MIXER_LINE:
681fd1aaeccSCameron Grant 		preg = 0x3e;
682fd1aaeccSCameron Grant 		rreg = 0x6e;
683fd1aaeccSCameron Grant 		break;
684fd1aaeccSCameron Grant 
685fd1aaeccSCameron Grant 	case SOUND_MIXER_MIC:
686fd1aaeccSCameron Grant 		preg = 0x1a;
687fd1aaeccSCameron Grant 		rreg = 0x68;
688fd1aaeccSCameron Grant 		break;
689fd1aaeccSCameron Grant 
690fd1aaeccSCameron Grant 	case SOUND_MIXER_LINE1:
691fd1aaeccSCameron Grant 		preg = 0x3a;
692fd1aaeccSCameron Grant 		rreg = 0x6c;
693fd1aaeccSCameron Grant 		break;
694fd1aaeccSCameron Grant 
695fd1aaeccSCameron Grant 	case SOUND_MIXER_CD:
696fd1aaeccSCameron Grant 		preg = 0x38;
697fd1aaeccSCameron Grant 		rreg = 0x6a;
698fd1aaeccSCameron Grant 		break;
699fd1aaeccSCameron Grant 
700fd1aaeccSCameron Grant 	case SOUND_MIXER_VOLUME:
701fd1aaeccSCameron Grant 		l = left? (left * 63) / 100 : 64;
702fd1aaeccSCameron Grant 		r = right? (right * 63) / 100 : 64;
703fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x60, l);
704fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x62, r);
705fd1aaeccSCameron Grant 		left = (l == 64)? 0 : (l * 100) / 63;
706fd1aaeccSCameron Grant 		right = (r == 64)? 0 : (r * 100) / 63;
707fd1aaeccSCameron Grant     		return left | (right << 8);
708fd1aaeccSCameron Grant 	}
709fd1aaeccSCameron Grant 
710fd1aaeccSCameron Grant 	if (preg)
711fd1aaeccSCameron Grant 		ess_setmixer(sc, preg, (l << 4) | r);
712fd1aaeccSCameron Grant 	if (rreg)
713fd1aaeccSCameron Grant 		ess_setmixer(sc, rreg, (l << 4) | r);
714fd1aaeccSCameron Grant 
715fd1aaeccSCameron Grant 	left = (l * 100) / 15;
716fd1aaeccSCameron Grant 	right = (r * 100) / 15;
717fd1aaeccSCameron Grant 
718fd1aaeccSCameron Grant     	return left | (right << 8);
719fd1aaeccSCameron Grant }
720fd1aaeccSCameron Grant 
721fd1aaeccSCameron Grant static int
722fd1aaeccSCameron Grant essmix_setrecsrc(snd_mixer *m, u_int32_t src)
723fd1aaeccSCameron Grant {
724fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
725fd1aaeccSCameron Grant     	u_char recdev;
726fd1aaeccSCameron Grant 
727fd1aaeccSCameron Grant     	switch (src) {
728fd1aaeccSCameron Grant 	case SOUND_MASK_CD:
729fd1aaeccSCameron Grant 		recdev = 0x02;
730fd1aaeccSCameron Grant 		break;
731fd1aaeccSCameron Grant 
732fd1aaeccSCameron Grant 	case SOUND_MASK_LINE:
733fd1aaeccSCameron Grant 		recdev = 0x06;
734fd1aaeccSCameron Grant 		break;
735fd1aaeccSCameron Grant 
736fd1aaeccSCameron Grant 	case SOUND_MASK_IMIX:
737fd1aaeccSCameron Grant 		recdev = 0x05;
738fd1aaeccSCameron Grant 		break;
739fd1aaeccSCameron Grant 
740fd1aaeccSCameron Grant 	case SOUND_MASK_MIC:
741fd1aaeccSCameron Grant 	default:
742fd1aaeccSCameron Grant 		recdev = 0x00;
743fd1aaeccSCameron Grant 		src = SOUND_MASK_MIC;
744fd1aaeccSCameron Grant 		break;
745fd1aaeccSCameron Grant 	}
746fd1aaeccSCameron Grant 
747fd1aaeccSCameron Grant 	ess_setmixer(sc, 0x1c, recdev);
748fd1aaeccSCameron Grant 
749fd1aaeccSCameron Grant 	return src;
750fd1aaeccSCameron Grant }
751fd1aaeccSCameron Grant 
752fd1aaeccSCameron Grant /************************************************************/
753fd1aaeccSCameron Grant 
754fd1aaeccSCameron Grant static int
755fd1aaeccSCameron Grant ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir)
756fd1aaeccSCameron Grant {
757fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
758fd1aaeccSCameron Grant 	sc->dmasz[ch - 1] = cnt;
759fd1aaeccSCameron Grant 	if (ch == 1) {
76019a0702eSNick Sayer 		port_wr(sc->vc, 0x8, 0xc4, 1); /* command */
761fd1aaeccSCameron Grant 		port_wr(sc->vc, 0xd, 0xff, 1); /* reset */
762fd1aaeccSCameron Grant 		port_wr(sc->vc, 0xf, 0x01, 1); /* mask */
76319a0702eSNick Sayer 		port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */
764fd1aaeccSCameron Grant 		port_wr(sc->vc, 0x0, base, 4);
765bb7f26c3SNick Sayer 		port_wr(sc->vc, 0x4, cnt - 1, 2);
766fd1aaeccSCameron Grant 
767fd1aaeccSCameron Grant 	} else if (ch == 2) {
768fd1aaeccSCameron Grant 		port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */
769fd1aaeccSCameron Grant 		port_wr(sc->io, 0x0, base, 4);
770fd1aaeccSCameron Grant 		port_wr(sc->io, 0x4, cnt, 2);
771fd1aaeccSCameron Grant 	}
772fd1aaeccSCameron Grant 	return 0;
773fd1aaeccSCameron Grant }
774fd1aaeccSCameron Grant 
775fd1aaeccSCameron Grant static int
776fd1aaeccSCameron Grant ess_dmapos(struct ess_info *sc, int ch)
777fd1aaeccSCameron Grant {
7786ba60b3cSNick Sayer 	int p = 0, i = 0, j = 0;
7790edeb3dcSNick Sayer 	u_long flags;
780fd1aaeccSCameron Grant 
781fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
7820edeb3dcSNick Sayer 	flags = spltty();
7830edeb3dcSNick Sayer 	if (ch == 1) {
7840edeb3dcSNick Sayer 
7850edeb3dcSNick Sayer /*
7860edeb3dcSNick Sayer  * During recording, this register is known to give back
7870edeb3dcSNick Sayer  * garbage if it's not quiescent while being read. That's
7886ba60b3cSNick Sayer  * why we spl, stop the DMA, and try over and over until
7896ba60b3cSNick Sayer  * adjacent reads are "close", in the right order and not
7906ba60b3cSNick Sayer  * bigger than is otherwise possible.
7910edeb3dcSNick Sayer  */
7920edeb3dcSNick Sayer 		ess_dmatrigger(sc, ch, 0);
7930edeb3dcSNick Sayer 		DELAY(20);
7946ba60b3cSNick Sayer 		do {
7956ba60b3cSNick Sayer 			DELAY(10);
7966ba60b3cSNick Sayer 			if (j > 1)
7976ba60b3cSNick Sayer 				printf("DMA count reg bogus: %04x & %04x\n",
7986ba60b3cSNick Sayer 					i, p);
7996ba60b3cSNick Sayer 			i = port_rd(sc->vc, 0x4, 2) + 1;
800fd1aaeccSCameron Grant 			p = port_rd(sc->vc, 0x4, 2) + 1;
8016ba60b3cSNick Sayer 		} while ((p > sc->dmasz[ch -1 ] || i < p || (p - i) > 0x8) && j++ < 1000);
8020edeb3dcSNick Sayer 		ess_dmatrigger(sc, ch, 1);
8030edeb3dcSNick Sayer 	}
804fd1aaeccSCameron Grant 	else if (ch == 2)
805fd1aaeccSCameron Grant 		p = port_rd(sc->io, 0x4, 2);
8060edeb3dcSNick Sayer 	splx(flags);
807fd1aaeccSCameron Grant 	return sc->dmasz[ch - 1] - p;
808fd1aaeccSCameron Grant }
809fd1aaeccSCameron Grant 
810fd1aaeccSCameron Grant static int
811fd1aaeccSCameron Grant ess_dmatrigger(struct ess_info *sc, int ch, int go)
812fd1aaeccSCameron Grant {
813fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
814fd1aaeccSCameron Grant 	if (ch == 1)
8158eb3acc9SNick Sayer 		port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */
816fd1aaeccSCameron Grant 	else if (ch == 2)
817fd1aaeccSCameron Grant 		port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */
818fd1aaeccSCameron Grant 	return 0;
819fd1aaeccSCameron Grant }
820fd1aaeccSCameron Grant 
821fd1aaeccSCameron Grant static void
822fd1aaeccSCameron Grant ess_release_resources(struct ess_info *sc, device_t dev)
823fd1aaeccSCameron Grant {
824fd1aaeccSCameron Grant     	/* should we bus_teardown_intr here? */
825fd1aaeccSCameron Grant     	if (sc->irq) {
826306f91b6SCameron Grant 		if (sc->ih)
827306f91b6SCameron Grant 			bus_teardown_intr(dev, sc->irq, sc->ih);
828fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
829fd1aaeccSCameron Grant 		sc->irq = 0;
830fd1aaeccSCameron Grant     	}
831fd1aaeccSCameron Grant     	if (sc->io) {
832fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IOPORT, 0 * 4 + PCIR_MAPS, sc->io);
833fd1aaeccSCameron Grant 		sc->io = 0;
834fd1aaeccSCameron Grant     	}
835fd1aaeccSCameron Grant 
836fd1aaeccSCameron Grant     	if (sc->sb) {
837fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IOPORT, 1 * 4 + PCIR_MAPS, sc->sb);
838fd1aaeccSCameron Grant 		sc->sb = 0;
839fd1aaeccSCameron Grant     	}
840fd1aaeccSCameron Grant 
841fd1aaeccSCameron Grant     	if (sc->vc) {
842fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IOPORT, 2 * 4 + PCIR_MAPS, sc->vc);
843fd1aaeccSCameron Grant 		sc->vc = 0;
844fd1aaeccSCameron Grant     	}
845fd1aaeccSCameron Grant 
846fd1aaeccSCameron Grant     	if (sc->mpu) {
847fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IOPORT, 3 * 4 + PCIR_MAPS, sc->mpu);
848fd1aaeccSCameron Grant 		sc->mpu = 0;
849fd1aaeccSCameron Grant     	}
850fd1aaeccSCameron Grant 
851fd1aaeccSCameron Grant     	if (sc->gp) {
852fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IOPORT, 4 * 4 + PCIR_MAPS, sc->gp);
853fd1aaeccSCameron Grant 		sc->gp = 0;
854fd1aaeccSCameron Grant     	}
855fd1aaeccSCameron Grant 
856306f91b6SCameron Grant 	if (sc->parent_dmat) {
857306f91b6SCameron Grant 		bus_dma_tag_destroy(sc->parent_dmat);
858306f91b6SCameron Grant 		sc->parent_dmat = 0;
859306f91b6SCameron Grant     	}
860306f91b6SCameron Grant 
861fd1aaeccSCameron Grant     	free(sc, M_DEVBUF);
862fd1aaeccSCameron Grant }
863fd1aaeccSCameron Grant 
864fd1aaeccSCameron Grant static int
865fd1aaeccSCameron Grant ess_alloc_resources(struct ess_info *sc, device_t dev)
866fd1aaeccSCameron Grant {
867fd1aaeccSCameron Grant 	int rid;
868fd1aaeccSCameron Grant 
869fd1aaeccSCameron Grant 	rid = 0 * 4 + PCIR_MAPS;
870fd1aaeccSCameron Grant     	sc->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
871fd1aaeccSCameron Grant 
872fd1aaeccSCameron Grant 	rid = 1 * 4 + PCIR_MAPS;
873fd1aaeccSCameron Grant     	sc->sb = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
874fd1aaeccSCameron Grant 
875fd1aaeccSCameron Grant 	rid = 2 * 4 + PCIR_MAPS;
876fd1aaeccSCameron Grant     	sc->vc = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
877fd1aaeccSCameron Grant 
878fd1aaeccSCameron Grant 	rid = 3 * 4 + PCIR_MAPS;
879fd1aaeccSCameron Grant     	sc->mpu = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
880fd1aaeccSCameron Grant 
881fd1aaeccSCameron Grant 	rid = 4 * 4 + PCIR_MAPS;
882fd1aaeccSCameron Grant     	sc->gp = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
883fd1aaeccSCameron Grant 
884fd1aaeccSCameron Grant 	rid = 0;
885fd1aaeccSCameron Grant 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
886fd1aaeccSCameron Grant 
887fd1aaeccSCameron Grant 	return (sc->irq && sc->io && sc->sb && sc->vc && sc->mpu && sc->gp)? 0 : ENXIO;
888fd1aaeccSCameron Grant }
889fd1aaeccSCameron Grant 
890fd1aaeccSCameron Grant static int
891fd1aaeccSCameron Grant ess_probe(device_t dev)
892fd1aaeccSCameron Grant {
893fd1aaeccSCameron Grant 	char *s = NULL;
894fd1aaeccSCameron Grant 	u_int32_t subdev;
895fd1aaeccSCameron Grant 
896fd1aaeccSCameron Grant 	subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
897fd1aaeccSCameron Grant 	switch (pci_get_devid(dev)) {
898fd1aaeccSCameron Grant 	case 0x1969125d:
899fd1aaeccSCameron Grant 		if (subdev == 0x8888125d)
900fd1aaeccSCameron Grant 			s = "ESS Solo-1E";
901fd1aaeccSCameron Grant 		else if (subdev == 0x1818125d)
902fd1aaeccSCameron Grant 			s = "ESS Solo-1";
903fd1aaeccSCameron Grant 		else
904fd1aaeccSCameron Grant 			s = "ESS Solo-1 (unknown vendor)";
905fd1aaeccSCameron Grant 		break;
906fd1aaeccSCameron Grant 	}
907fd1aaeccSCameron Grant 
908fd1aaeccSCameron Grant 	if (s)
909fd1aaeccSCameron Grant 		device_set_desc(dev, s);
910fd1aaeccSCameron Grant 	return s? 0 : ENXIO;
911fd1aaeccSCameron Grant }
912fd1aaeccSCameron Grant 
913fd1aaeccSCameron Grant #define PCI_LEGACYCONTROL       0x40
914fd1aaeccSCameron Grant #define PCI_CONFIG              0x50
915fd1aaeccSCameron Grant #define PCI_DDMACONTROL      	0x60
916fd1aaeccSCameron Grant 
917fd1aaeccSCameron Grant static int
918fd1aaeccSCameron Grant ess_attach(device_t dev)
919fd1aaeccSCameron Grant {
920fd1aaeccSCameron Grant     	struct ess_info *sc;
921fd1aaeccSCameron Grant     	char status[SND_STATUSLEN];
922fd1aaeccSCameron Grant 	u_int16_t ddma;
923fd1aaeccSCameron Grant 	u_int32_t data;
924fd1aaeccSCameron Grant 
925fd1aaeccSCameron Grant 	sc = (struct ess_info *)malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
926fd1aaeccSCameron Grant     	if (!sc)
927fd1aaeccSCameron Grant 		return ENXIO;
928fd1aaeccSCameron Grant     	bzero(sc, sizeof *sc);
929fd1aaeccSCameron Grant 
930fd1aaeccSCameron Grant 	data = pci_read_config(dev, PCIR_COMMAND, 2);
931fd1aaeccSCameron Grant 	data |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
932fd1aaeccSCameron Grant 	pci_write_config(dev, PCIR_COMMAND, data, 2);
933fd1aaeccSCameron Grant 	data = pci_read_config(dev, PCIR_COMMAND, 2);
934fd1aaeccSCameron Grant 
935fd1aaeccSCameron Grant     	if (ess_alloc_resources(sc, dev))
936fd1aaeccSCameron Grant 		goto no;
937fd1aaeccSCameron Grant 
938fd1aaeccSCameron Grant 	ddma = rman_get_start(sc->vc) | 1;
939fd1aaeccSCameron Grant 	pci_write_config(dev, PCI_LEGACYCONTROL, 0x805f, 2);
940fd1aaeccSCameron Grant 	pci_write_config(dev, PCI_DDMACONTROL, ddma, 2);
941fd1aaeccSCameron Grant 	pci_write_config(dev, PCI_CONFIG, 0, 2);
942fd1aaeccSCameron Grant 
943fd1aaeccSCameron Grant     	if (ess_reset_dsp(sc))
944fd1aaeccSCameron Grant 		goto no;
94533dbf14aSCameron Grant     	mixer_init(dev, &ess_mixer, sc);
946fd1aaeccSCameron Grant 
947fd1aaeccSCameron Grant 	port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
9483ac1ca33SNick Sayer #ifdef ESS18XX_DUPLEX
949fd1aaeccSCameron Grant 	sc->duplex = 1;
9503ac1ca33SNick Sayer #else
9513ac1ca33SNick Sayer 	sc->duplex = 0;
9523ac1ca33SNick Sayer #endif
953fd1aaeccSCameron Grant 
9543ac1ca33SNick Sayer #ifdef ESS18XX_NEWSPEED
9553ac1ca33SNick Sayer 	sc->newspeed = 1;
9563ac1ca33SNick Sayer #else
9573ac1ca33SNick Sayer 	sc->newspeed = 0;
9583ac1ca33SNick Sayer #endif
959fd1aaeccSCameron Grant 	if (sc->newspeed)
960fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x71, 0x2a);
961fd1aaeccSCameron Grant 
962306f91b6SCameron Grant 	bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY, ess_intr, sc, &sc->ih);
963fd1aaeccSCameron Grant     	if (!sc->duplex)
964fd1aaeccSCameron Grant 		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
965fd1aaeccSCameron Grant 
966bb7f26c3SNick Sayer     	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/65536, /*boundary*/0,
967fd1aaeccSCameron Grant 			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
968fd1aaeccSCameron Grant 			/*highaddr*/BUS_SPACE_MAXADDR,
969fd1aaeccSCameron Grant 			/*filter*/NULL, /*filterarg*/NULL,
970fd1aaeccSCameron Grant 			/*maxsize*/ESS_BUFFSIZE, /*nsegments*/1,
971fd1aaeccSCameron Grant 			/*maxsegz*/0x3ffff,
972fd1aaeccSCameron Grant 			/*flags*/0, &sc->parent_dmat) != 0) {
973fd1aaeccSCameron Grant 		device_printf(dev, "unable to create dma tag\n");
974fd1aaeccSCameron Grant 		goto no;
975fd1aaeccSCameron Grant     	}
976fd1aaeccSCameron Grant 
977fd1aaeccSCameron Grant     	snprintf(status, SND_STATUSLEN, "at io 0x%lx,0x%lx,0x%lx irq %ld",
978fd1aaeccSCameron Grant     	     	rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc),
979fd1aaeccSCameron Grant 		rman_get_start(sc->irq));
980fd1aaeccSCameron Grant 
981fd1aaeccSCameron Grant     	if (pcm_register(dev, sc, 1, 1))
982fd1aaeccSCameron Grant 		goto no;
983fd1aaeccSCameron Grant       	pcm_addchan(dev, PCMDIR_REC, &ess_chantemplate, sc);
984fd1aaeccSCameron Grant 	pcm_addchan(dev, PCMDIR_PLAY, &ess_chantemplate, sc);
985fd1aaeccSCameron Grant 	pcm_setstatus(dev, status);
986fd1aaeccSCameron Grant 
987fd1aaeccSCameron Grant     	return 0;
988fd1aaeccSCameron Grant 
989fd1aaeccSCameron Grant no:
990fd1aaeccSCameron Grant     	ess_release_resources(sc, dev);
991fd1aaeccSCameron Grant     	return ENXIO;
992fd1aaeccSCameron Grant }
993fd1aaeccSCameron Grant 
994306f91b6SCameron Grant static int
995306f91b6SCameron Grant ess_detach(device_t dev)
996306f91b6SCameron Grant {
997306f91b6SCameron Grant 	int r;
998306f91b6SCameron Grant 	struct sc_info *sc;
999306f91b6SCameron Grant 
1000306f91b6SCameron Grant 	r = pcm_unregister(dev);
1001306f91b6SCameron Grant 	if (r)
1002306f91b6SCameron Grant 		return r;
1003306f91b6SCameron Grant 
1004306f91b6SCameron Grant 	sc = pcm_getdevinfo(dev);
1005306f91b6SCameron Grant     	ess_release_resources(sc, dev);
1006306f91b6SCameron Grant 	return 0;
1007306f91b6SCameron Grant }
1008306f91b6SCameron Grant 
1009fd1aaeccSCameron Grant static device_method_t ess_methods[] = {
1010fd1aaeccSCameron Grant 	/* Device interface */
1011fd1aaeccSCameron Grant 	DEVMETHOD(device_probe,		ess_probe),
1012fd1aaeccSCameron Grant 	DEVMETHOD(device_attach,	ess_attach),
1013306f91b6SCameron Grant 	DEVMETHOD(device_detach,	ess_detach),
1014fd1aaeccSCameron Grant 
1015fd1aaeccSCameron Grant 	{ 0, 0 }
1016fd1aaeccSCameron Grant };
1017fd1aaeccSCameron Grant 
1018fd1aaeccSCameron Grant static driver_t ess_driver = {
1019fd1aaeccSCameron Grant 	"pcm",
1020fd1aaeccSCameron Grant 	ess_methods,
1021fd1aaeccSCameron Grant 	sizeof(snddev_info),
1022fd1aaeccSCameron Grant };
1023fd1aaeccSCameron Grant 
1024fd1aaeccSCameron Grant DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, 0, 0);
1025fd1aaeccSCameron Grant MODULE_DEPEND(snd_solo, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
1026fd1aaeccSCameron Grant MODULE_VERSION(snd_solo, 1);
1027fd1aaeccSCameron Grant 
1028fd1aaeccSCameron Grant 
1029fd1aaeccSCameron Grant 
1030