xref: /openbsd-src/sys/dev/pci/maestro.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: maestro.c,v 1.8 2001/08/02 11:16:29 espie Exp $	*/
2 /* $FreeBSD: /c/ncvs/src/sys/dev/sound/pci/maestro.c,v 1.3 2000/11/21 12:22:11 julian Exp $ */
3 /*
4  * FreeBSD's ESS Agogo/Maestro driver
5  * Converted from FreeBSD's pcm to OpenBSD's audio.
6  * Copyright (c) 2000, 2001 David Leonard & Marc Espie
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*-
31  * (FreeBSD) Credits:
32  * Copyright (c) 2000 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
33  *
34  * Part of this code (especially in many magic numbers) was heavily inspired
35  * by the Linux driver originally written by
36  * Alan Cox <alan.cox@linux.org>, modified heavily by
37  * Zach Brown <zab@zabbo.net>.
38  *
39  * busdma()-ize and buffer size reduction were suggested by
40  * Cameron Grant <gandalf@vilnya.demon.co.uk>.
41  * Also he showed me the way to use busdma() suite.
42  *
43  * Internal speaker problems on NEC VersaPro's and Dell Inspiron 7500
44  * were looked at by
45  * Munehiro Matsuda <haro@tk.kubota.co.jp>,
46  * who brought patches based on the Linux driver with some simplification.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/device.h>
54 #include <sys/proc.h>
55 #include <sys/queue.h>
56 #include <sys/fcntl.h>
57 
58 #include <dev/pci/pcidevs.h>
59 #include <dev/pci/pcivar.h>
60 
61 #include <sys/audioio.h>
62 #include <dev/audio_if.h>
63 #include <dev/mulaw.h>
64 #include <dev/auconv.h>
65 
66 #include <dev/ic/ac97.h>
67 #include <dev/pci/maestro_reg.h>
68 
69 #ifdef AUDIO_DEBUG
70 #define DPRINTF(x)	if (maestrodebug) printf x
71 #define DLPRINTF(i, x)	if (maestrodebug & i) printf x
72 int	maestrodebug = 0;
73 u_long maestrointr_called;
74 u_long maestrodma_effective;
75 
76 #define MAESTRODEBUG_INTR 1
77 #define MAESTRODEBUG_TIMER 2
78 #else
79 #define DPRINTF(x)
80 #define DLPRINTF(i, x)
81 #endif
82 
83 #define MAESTRO_BUFSIZ		0x4000
84 #define lengthof(array)		(sizeof (array) / sizeof (array)[0])
85 
86 #define STEP_VOLUME		0x22
87 #define MIDDLE_VOLUME		(STEP_VOLUME * 4)
88 
89 typedef struct salloc_pool {
90 	struct salloc_zone {
91 		SLIST_ENTRY(salloc_zone) link;
92 		caddr_t		addr;
93 		size_t		size;
94 	} *zones;
95 	SLIST_HEAD(salloc_head, salloc_zone) free, used, spare;
96 } *salloc_t;
97 
98 struct maestro_softc;
99 
100 #define MAESTRO_PLAY	1
101 #define MAESTRO_STEREO	2
102 #define MAESTRO_8BIT	4
103 #define MAESTRO_UNSIGNED	8
104 #define MAESTRO_RUNNING	16
105 
106 struct maestro_channel {
107 	struct maestro_softc 	*sc;
108 	int			num;
109 	u_int32_t		blocksize;
110 	u_int16_t		mode;
111 	u_int32_t		speed;
112 	u_int32_t		dv;
113 	u_int16_t		start;
114 	u_int16_t		threshold;
115 	u_int16_t		end;
116 	u_int16_t		current;
117 	u_int			wpwa;
118 	void			(*intr) __P((void *));
119 	void			*intr_arg;
120 };
121 
122 struct maestro_softc {
123 	struct device		dev;
124 
125 	void			*ih;
126 	pci_chipset_tag_t	pc;
127 	pcitag_t		pt;
128 
129 #define MAESTRO_FLAG_SETUPGPIO	0x0001
130 	int			flags;
131 	bus_space_tag_t		iot;
132 	bus_space_handle_t	ioh;
133 	bus_dma_tag_t		dmat;
134 
135 	caddr_t			dmabase;
136 	bus_addr_t		physaddr;
137 	size_t			dmasize;
138 	bus_dmamap_t		dmamap;
139 	bus_dma_segment_t	dmaseg;
140 	salloc_t		dmapool;
141 
142 	struct ac97_codec_if	*codec_if;
143 	struct ac97_host_if	host_if;
144 	struct audio_device	*sc_audev;
145 
146 	void			*powerhook;
147 	int			suspend;
148 
149 	struct maestro_channel	play;
150 	struct maestro_channel	record;
151 };
152 
153 
154 typedef	u_int16_t wpreg_t;
155 typedef	u_int16_t wcreg_t;
156 
157 salloc_t salloc_new __P((caddr_t, size_t, int));
158 void	salloc_destroy __P((salloc_t));
159 caddr_t	salloc_alloc __P((salloc_t, size_t));
160 void	salloc_free __P((salloc_t, caddr_t));
161 void	salloc_insert __P((salloc_t, struct salloc_head *,
162 		struct salloc_zone *, int));
163 
164 int	maestro_match __P((struct device *, void *, void *));
165 void	maestro_attach __P((struct device *, struct device *, void *));
166 int	maestro_intr __P((void *));
167 
168 int	maestro_open __P((void *, int));
169 void	maestro_close __P((void *));
170 int	maestro_query_encoding __P((void *, struct audio_encoding *));
171 int	maestro_set_params __P((void *, int, int, struct audio_params *,
172 			    struct audio_params *));
173 int	maestro_round_blocksize __P((void *, int));
174 int	maestro_halt_output __P((void *));
175 int	maestro_halt_input __P((void *));
176 int	maestro_getdev __P((void *, struct audio_device *));
177 int	maestro_set_port __P((void *, mixer_ctrl_t *));
178 int	maestro_get_port __P((void *, mixer_ctrl_t *));
179 int	maestro_query_devinfo __P((void *, mixer_devinfo_t *));
180 void	*maestro_malloc __P((void *, int, size_t, int, int));
181 void	maestro_free __P((void *, void *, int));
182 size_t	maestro_round_buffersize __P((void *, int, size_t));
183 int	maestro_mappage __P((void *, void *, int, int));
184 int	maestro_get_props __P((void *));
185 int	maestro_trigger_output __P((void *, void *, void *, int, void (*)(void *),
186 				void *, struct audio_params *));
187 int	maestro_trigger_input __P((void *, void *, void *, int, void (*)(void *),
188 			       void *, struct audio_params *));
189 
190 int	maestro_attach_codec __P((void *, struct ac97_codec_if *));
191 int	maestro_read_codec __P((void *, u_int8_t, u_int16_t *));
192 int	maestro_write_codec __P((void *, u_int8_t, u_int16_t));
193 void	maestro_reset_codec __P((void *));
194 
195 void	maestro_initcodec __P((void *));
196 
197 void	maestro_set_speed __P((struct maestro_channel *, u_long *));
198 void	maestro_init __P((struct maestro_softc *));
199 void	maestro_power __P((struct maestro_softc *, int));
200 void	maestro_powerhook __P((int, void *));
201 
202 void 	maestro_channel_start __P((struct maestro_channel *));
203 void 	maestro_channel_stop __P((struct maestro_channel *));
204 void 	maestro_channel_advance_dma __P((struct maestro_channel *));
205 
206 int	maestro_get_flags __P((struct pci_attach_args *));
207 
208 void	ringbus_setdest __P((struct maestro_softc *, int, int));
209 
210 wpreg_t	wp_reg_read __P((struct maestro_softc *, int));
211 void	wp_reg_write __P((struct maestro_softc *, int, wpreg_t));
212 wpreg_t	wp_apu_read __P((struct maestro_softc *, int, int));
213 void	wp_apu_write __P((struct maestro_softc *, int, int, wpreg_t));
214 void	wp_settimer __P((struct maestro_softc *, u_int));
215 void	wp_starttimer __P((struct maestro_softc *));
216 void	wp_stoptimer __P((struct maestro_softc *));
217 
218 wcreg_t	wc_reg_read __P((struct maestro_softc *, int));
219 void	wc_reg_write __P((struct maestro_softc *, int, wcreg_t));
220 wcreg_t	wc_ctrl_read __P((struct maestro_softc *, int));
221 void	wc_ctrl_write __P((struct maestro_softc *, int, wcreg_t));
222 
223 u_int maestro_calc_timer_freq __P((struct maestro_channel *));
224 void maestro_update_timer __P((struct maestro_softc *));
225 
226 struct cfdriver maestro_cd = {
227 	NULL, "maestro", DV_DULL
228 };
229 
230 struct cfattach maestro_ca = {
231 	sizeof (struct maestro_softc), maestro_match, maestro_attach
232 };
233 
234 struct audio_hw_if maestro_hw_if = {
235 	maestro_open,
236 	maestro_close,
237 	NULL,
238 	maestro_query_encoding,
239 	maestro_set_params,
240 	maestro_round_blocksize,
241 	NULL,
242 	NULL,
243 	NULL,
244 	NULL,
245 	NULL,
246 	maestro_halt_output,
247 	maestro_halt_input,
248 	NULL,
249 	maestro_getdev,
250 	NULL,
251 	maestro_set_port,
252 	maestro_get_port,
253 	maestro_query_devinfo,
254 	NULL,
255 	maestro_free,
256 	NULL,
257 	maestro_mappage,
258 	maestro_get_props,
259 	maestro_trigger_output,
260 	maestro_trigger_input,
261 	maestro_malloc,
262 	maestro_round_buffersize
263 };
264 
265 struct audio_device maestro_audev = {
266 	"ESS Maestro", "", "maestro"
267 };
268 
269 struct {
270 	u_short vendor, product;
271 	int flags;
272 } maestro_pcitab[] = {
273 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTROII,	0 },
274 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO2E,	0 },
275 	{ PCI_VENDOR_PLATFORM, PCI_PRODUCT_PLATFORM_ES1849,	0 },
276 	{ PCI_VENDOR_NEC, PCI_PRODUCT_NEC_VERSAMAESTRO,		MAESTRO_FLAG_SETUPGPIO },
277 	{ PCI_VENDOR_NEC, PCI_PRODUCT_NEC_VERSAPRONXVA26D,	MAESTRO_FLAG_SETUPGPIO }
278 };
279 #define NMAESTRO_PCITAB	lengthof(maestro_pcitab)
280 
281 int
282 maestro_get_flags(pa)
283 	struct pci_attach_args *pa;
284 {
285 	int i;
286 
287 	/* Distinguish audio devices from modems with the same manfid */
288 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_MULTIMEDIA)
289 		return (-1);
290 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MULTIMEDIA_AUDIO)
291 		return (-1);
292 	for (i = 0; i < NMAESTRO_PCITAB; i++)
293 		if (PCI_VENDOR(pa->pa_id) == maestro_pcitab[i].vendor &&
294 		    PCI_PRODUCT(pa->pa_id) == maestro_pcitab[i].product)
295 			return (maestro_pcitab[i].flags);
296 	return (-1);
297 }
298 
299 /* -----------------------------
300  * Driver interface.
301  */
302 
303 int
304 maestro_match(parent, match, aux)
305 	struct device *parent;
306 	void *match;
307 	void *aux;
308 {
309 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
310 
311 	if (maestro_get_flags(pa) == -1)
312 		return (0);
313 	else
314 		return (1);
315 }
316 
317 void
318 maestro_attach(parent, self, aux)
319 	struct device *parent;
320 	struct device *self;
321 	void *aux;
322 {
323 	struct maestro_softc *sc = (struct maestro_softc *)self;
324 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
325 	pci_chipset_tag_t pc = pa->pa_pc;
326 	char const *intrstr;
327 	pci_intr_handle_t ih;
328 	int error;
329 	pcireg_t data;
330 	u_int16_t cdata;
331 	int dmastage = 0;
332 	int rseg;
333 
334 	sc->sc_audev = &maestro_audev;
335 	sc->flags = maestro_get_flags(pa);
336 
337 	sc->pc = pa->pa_pc;
338 	sc->pt = pa->pa_tag;
339 	sc->dmat = pa->pa_dmat;
340 
341 	/* Map interrupt */
342 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
343 	    &ih)) {
344 		printf(": couldn't map interrupt\n");
345 		return;
346 	}
347 	intrstr = pci_intr_string(pc, ih);
348 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, maestro_intr, sc,
349 	    sc->dev.dv_xname);
350 	if (sc->ih == NULL) {
351 		printf(": couldn't establish interrupt");
352 		if (intrstr != NULL)
353 			printf(" at %s\n", intrstr);
354 		return;
355 	}
356 	printf(": %s", intrstr);
357 
358 	/* Rangers, power up */
359 	maestro_power(sc, PPMI_D0);
360 	DELAY(100000);
361 
362 	/* Map i/o */
363 	if ((error = pci_mapreg_map(pa, PCI_MAPS, PCI_MAPREG_TYPE_IO,
364 	    0, &sc->iot, &sc->ioh, NULL, NULL, 0)) != 0) {
365 		printf(", couldn't map i/o space\n");
366 		goto bad;
367 	};
368 
369 	/* Enable bus mastering */
370 	data = pci_conf_read(sc->pc, sc->pt, PCI_COMMAND_STATUS_REG);
371 	if ((data & PCI_COMMAND_MASTER_ENABLE) == 0)
372 		pci_conf_write(sc->pc, sc->pt, PCI_COMMAND_STATUS_REG,
373 			data | PCI_COMMAND_MASTER_ENABLE);
374 
375 	/* Allocate fixed DMA segment :-( */
376 	sc->dmasize = MAESTRO_BUFSIZ * 16;
377 	if ((error = bus_dmamem_alloc(sc->dmat, sc->dmasize, NBPG, 0,
378 	    &sc->dmaseg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
379 		printf(", unable to alloc dma, error %d\n", error);
380 		goto bad;
381 	}
382 	dmastage = 1;
383 	if ((error = bus_dmamem_map(sc->dmat, &sc->dmaseg, 1,
384 	    sc->dmasize, &sc->dmabase, BUS_DMA_NOWAIT |
385 	    BUS_DMA_COHERENT)) != 0) {
386 		printf(", unable to map dma, error %d\n", error);
387 		goto bad;
388 	}
389 	dmastage = 2;
390 	if ((error = bus_dmamap_create(sc->dmat, sc->dmasize, 1,
391 	    sc->dmasize, 0, BUS_DMA_NOWAIT, &sc->dmamap)) != 0) {
392 		printf(", unable to create dma map, error %d\n", error);
393 		goto bad;
394 	}
395 	dmastage = 3;
396 	if ((error = bus_dmamap_load(sc->dmat, sc->dmamap,
397 	    sc->dmabase, sc->dmasize, NULL, BUS_DMA_NOWAIT)) != 0) {
398 		printf(", unable to load dma map, error %d\n", error);
399 		goto bad;
400 	}
401 
402 	/* XXX
403 	 * The first byte of the allocated memory is not usable,
404 	 * the WP sometimes uses it to store status.
405 	 */
406 	/* Make DMA memory pool */
407 	if ((sc->dmapool = salloc_new(sc->dmabase+16, sc->dmasize-16,
408 	    128/*overkill?*/)) == NULL) {
409 		printf(", unable to make dma pool\n");
410 		goto bad;
411 	}
412 
413 	sc->physaddr = sc->dmamap->dm_segs[0].ds_addr;
414 
415 	printf("\n");
416 
417 	/* Kick device */
418 	maestro_init(sc);
419 	maestro_read_codec(sc, 0, &cdata);
420 	if (cdata == 0x80) {
421 		printf("%s: PT101 codec unsupported, no mixer\n",
422 		    sc->dev.dv_xname);
423 		/* Init values from Linux, no idea what this does. */
424 		maestro_write_codec(sc, 0x2a, 0x0001);
425 		maestro_write_codec(sc, 0x2C, 0x0000);
426 		maestro_write_codec(sc, 0x2C, 0xFFFF);
427 		maestro_write_codec(sc, 0x10, 0x9F1F);
428 		maestro_write_codec(sc, 0x12, 0x0808);
429 		maestro_write_codec(sc, 0x14, 0x9F1F);
430 		maestro_write_codec(sc, 0x16, 0x9F1F);
431 		maestro_write_codec(sc, 0x18, 0x0404);
432 		maestro_write_codec(sc, 0x1A, 0x0000);
433 		maestro_write_codec(sc, 0x1C, 0x0000);
434 		maestro_write_codec(sc, 0x02, 0x0404);
435 		maestro_write_codec(sc, 0x04, 0x0808);
436 		maestro_write_codec(sc, 0x0C, 0x801F);
437 		maestro_write_codec(sc, 0x0E, 0x801F);
438 		/* no control over the mixer, sorry */
439 		sc->codec_if = NULL;
440 	} else {
441 		/* Attach the AC'97 */
442 		sc->host_if.arg = sc;
443 		sc->host_if.attach = maestro_attach_codec;
444 		sc->host_if.read = maestro_read_codec;
445 		sc->host_if.write = maestro_write_codec;
446 		sc->host_if.reset = maestro_reset_codec;
447 		if (ac97_attach(&sc->host_if) != 0) {
448 			printf("%s: couldn't attach codec\n", sc->dev.dv_xname);
449 			goto bad;
450 		}
451 	}
452 
453 	sc->play.mode = MAESTRO_PLAY;
454 	sc->play.sc = sc;
455 	sc->play.num = 0;
456 	sc->record.sc = sc;
457 	sc->record.num = 2;
458 	sc->record.mode = 0;
459 
460 	/* Attach audio */
461 	audio_attach_mi(&maestro_hw_if, sc, &sc->dev);
462 
463 	/* Hook power changes */
464 	sc->suspend = PWR_RESUME;
465 	sc->powerhook = powerhook_establish(maestro_powerhook, sc);
466 
467 	return;
468 
469  bad:
470 	/* Power down. */
471 	maestro_power(sc, PPMI_D3);
472 	if (sc->ih)
473 		pci_intr_disestablish(pc, sc->ih);
474 	printf("%s: disabled\n", sc->dev.dv_xname);
475 	if (sc->dmapool)
476 		salloc_destroy(sc->dmapool);
477 	if (dmastage >= 3)
478 		bus_dmamap_destroy(sc->dmat, sc->dmamap);
479 	if (dmastage >= 2)
480 		bus_dmamem_unmap(sc->dmat, sc->dmabase, sc->dmasize);
481 	if (dmastage >= 1)
482 		bus_dmamem_free(sc->dmat, &sc->dmaseg, 1);
483 }
484 
485 void
486 maestro_init(sc)
487 	struct maestro_softc *sc;
488 {
489 	int reg;
490 	pcireg_t data;
491 
492 	/* Disable all legacy emulations. */
493 	data = pci_conf_read(sc->pc, sc->pt, CONF_LEGACY);
494 	data |= LEGACY_DISABLED;
495 	pci_conf_write(sc->pc, sc->pt, CONF_LEGACY, data);
496 
497 	/* Disconnect from CHI. (Makes Dell inspiron 7500 work?)
498 	 * Enable posted write.
499 	 * Prefer PCI timing rather than that of ISA.
500 	 * Don't swap L/R. */
501 	data = pci_conf_read(sc->pc, sc->pt, CONF_MAESTRO);
502 	data |= MAESTRO_CHIBUS | MAESTRO_POSTEDWRITE | MAESTRO_DMA_PCITIMING;
503 	data &= ~MAESTRO_SWAP_LR;
504 	pci_conf_write(sc->pc, sc->pt, CONF_MAESTRO, data);
505 	/* Reset direct sound. */
506 	bus_space_write_2(sc->iot, sc->ioh, PORT_HOSTINT_CTRL,
507 	    HOSTINT_CTRL_DSOUND_RESET);
508 	DELAY(10000);	/* XXX - too long? */
509 	bus_space_write_2(sc->iot, sc->ioh, PORT_HOSTINT_CTRL, 0);
510 	DELAY(10000);
511 
512 	/* Enable direct sound and hardware volume control interruptions. */
513 	bus_space_write_2(sc->iot, sc->ioh, PORT_HOSTINT_CTRL,
514 	    HOSTINT_CTRL_DSOUND_INT_ENABLED | HOSTINT_CTRL_HWVOL_ENABLED);
515 
516 	/* Setup Wave Processor. */
517 
518 	/* Enable WaveCache, set DMA base address. */
519 	wp_reg_write(sc, WPREG_WAVE_ROMRAM,
520 	    WP_WAVE_VIRTUAL_ENABLED | WP_WAVE_DRAM_ENABLED);
521 	bus_space_write_2(sc->iot, sc->ioh, PORT_WAVCACHE_CTRL,
522 	    WAVCACHE_ENABLED | WAVCACHE_WTSIZE_4MB);
523 
524 	for (reg = WAVCACHE_PCMBAR; reg < WAVCACHE_PCMBAR + 4; reg++)
525 		wc_reg_write(sc, reg,
526 			sc->physaddr >> WAVCACHE_BASEADDR_SHIFT);
527 
528 	/* Setup Codec/Ringbus. */
529 	maestro_initcodec(sc);
530 	bus_space_write_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL,
531 	    RINGBUS_CTRL_RINGBUS_ENABLED | RINGBUS_CTRL_ACLINK_ENABLED);
532 
533 	wp_reg_write(sc, WPREG_BASE, 0x8500);	/* Parallel I/O */
534 	ringbus_setdest(sc, RINGBUS_SRC_ADC,
535 	    RINGBUS_DEST_STEREO | RINGBUS_DEST_DSOUND_IN);
536 	ringbus_setdest(sc, RINGBUS_SRC_DSOUND,
537 	    RINGBUS_DEST_STEREO | RINGBUS_DEST_DAC);
538 
539 	/* Setup ASSP. Needed for Dell Inspiron 7500? */
540 	bus_space_write_1(sc->iot, sc->ioh, PORT_ASSP_CTRL_B, 0x00);
541 	bus_space_write_1(sc->iot, sc->ioh, PORT_ASSP_CTRL_A, 0x03);
542 	bus_space_write_1(sc->iot, sc->ioh, PORT_ASSP_CTRL_C, 0x00);
543 
544 	/*
545 	 * Reset hw volume to a known value so that we may handle diffs
546 	 * off to AC'97.
547 	 */
548 
549 	bus_space_write_1(sc->iot, sc->ioh, PORT_HWVOL_MASTER, MIDDLE_VOLUME);
550 	/* Setup GPIO if needed (NEC systems) */
551 	if (sc->flags & MAESTRO_FLAG_SETUPGPIO) {
552 		/* Matthew Braithwaite <matt@braithwaite.net> reported that
553 		 * NEC Versa LX doesn't need GPIO operation. */
554 		bus_space_write_2(sc->iot, sc->ioh,
555 		    PORT_GPIO_MASK, 0x9ff);
556 		bus_space_write_2(sc->iot, sc->ioh, PORT_GPIO_DIR,
557 		    bus_space_read_2(sc->iot, sc->ioh, PORT_GPIO_DIR) | 0x600);
558 		bus_space_write_2(sc->iot, sc->ioh,
559 		    PORT_GPIO_DATA, 0x200);
560 	}
561 }
562 
563 /* -----------------------------
564  * Audio interface
565  */
566 
567 int
568 maestro_round_blocksize(self, blk)
569 	void *self;
570 	int blk;
571 {
572 	return (blk & ~0xf);
573 }
574 
575 size_t
576 maestro_round_buffersize(self, direction, size)
577 	void *self;
578 	int direction;
579 	size_t size;
580 {
581 	return (size);
582 }
583 
584 void *
585 maestro_malloc(arg, dir, size, pool, flags)
586 	void *arg;
587 	int dir;
588 	size_t size;
589 	int pool, flags;
590 {
591 	struct maestro_softc *sc = (struct maestro_softc *)arg;
592 
593 	return (salloc_alloc(sc->dmapool, size));
594 }
595 
596 void
597 maestro_free(self, ptr, pool)
598 	void *self, *ptr;
599 	int pool;
600 {
601 	struct maestro_softc *sc = (struct maestro_softc *)self;
602 
603 	salloc_free(sc->dmapool, ptr);
604 }
605 
606 int
607 maestro_mappage(self, mem, off, prot)
608 	void *self, *mem;
609 	int off, prot;
610 {
611 	struct maestro_softc *sc = (struct maestro_softc *)self;
612 
613 	if (off < 0)
614 		return -1;
615 	return bus_dmamem_mmap(sc->dmat, &sc->dmaseg, 1,
616 		off, prot, BUS_DMA_WAITOK);
617 }
618 
619 int
620 maestro_get_props(self)
621 	void *self;
622 {
623 	/* struct maestro_softc *sc = (struct maestro_softc *)self; */
624 
625 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT); /* XXX */
626 }
627 
628 int
629 maestro_getdev(self, retp)
630 	void *self;
631 	struct audio_device *retp;
632 {
633 	struct maestro_softc *sc = (struct maestro_softc *)self;
634 
635 	*retp = *sc->sc_audev;
636 	return 0;
637 }
638 
639 int
640 maestro_set_port(self, cp)
641 	void *self;
642 	mixer_ctrl_t *cp;
643 {
644 	struct ac97_codec_if *c = ((struct maestro_softc *)self)->codec_if;
645 
646 	if (c)
647 		return (c->vtbl->mixer_set_port(c, cp));
648 	else
649 		return (ENXIO);
650 }
651 
652 int
653 maestro_get_port(self, cp)
654 	void *self;
655 	mixer_ctrl_t *cp;
656 {
657 	struct ac97_codec_if *c = ((struct maestro_softc *)self)->codec_if;
658 
659 	if (c)
660 		return (c->vtbl->mixer_get_port(c, cp));
661 	else
662 		return (ENXIO);
663 }
664 
665 int
666 maestro_query_devinfo(self, cp)
667 	void *self;
668 	mixer_devinfo_t *cp;
669 {
670 	struct ac97_codec_if *c = ((struct maestro_softc *)self)->codec_if;
671 
672 	if (c)
673 		return (c->vtbl->query_devinfo(c, cp));
674 	else
675 		return (ENXIO);
676 }
677 
678 struct audio_encoding maestro_tab[] = {
679 	{0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0},
680 	{1, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8, 0},
681 	{2, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0},
682 	{3, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16,
683 	    AUDIO_ENCODINGFLAG_EMULATED},
684 	{4, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16,
685 	    AUDIO_ENCODINGFLAG_EMULATED},
686 	{5, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16,
687 	    AUDIO_ENCODINGFLAG_EMULATED},
688 	{6, AudioEmulaw, AUDIO_ENCODING_ULAW, 8,
689 	    AUDIO_ENCODINGFLAG_EMULATED},
690 	{7, AudioEalaw, AUDIO_ENCODING_ALAW, 8,
691 	    AUDIO_ENCODINGFLAG_EMULATED}
692 };
693 
694 int
695 maestro_query_encoding(hdl, fp)
696 	void *hdl;
697 	struct audio_encoding *fp;
698 {
699 	if (fp->index < 0 || fp->index >= lengthof(maestro_tab))
700 		return (EINVAL);
701 	*fp = maestro_tab[fp->index];
702 	return (0);
703 }
704 
705 #define UNUSED __attribute__((unused))
706 
707 void
708 maestro_set_speed(ch, prate)
709 	struct maestro_channel *ch;
710 	u_long *prate;
711 {
712 	ch->speed = *prate;
713 	if ((ch->mode & (MAESTRO_8BIT | MAESTRO_STEREO)) == MAESTRO_8BIT)
714 		ch->speed /= 2;
715 
716 	/* special common case */
717 	if (ch->speed == 48000) {
718 		ch->dv = 0x10000;
719 	} else {
720 		/* compute 16 bits fixed point value of speed/48000,
721 		 * being careful not to overflow */
722 		 ch->dv = (((ch->speed % 48000) << 16U) + 24000) / 48000
723 		    + ((ch->speed / 48000) << 16U);
724 		/* And this is the real rate obtained */
725 		ch->speed = (ch->dv >> 16U) * 48000 +
726 		    (((ch->dv & 0xffff)*48000)>>16U);
727 	}
728 	*prate = ch->speed;
729 	if ((ch->mode & (MAESTRO_8BIT | MAESTRO_STEREO)) == MAESTRO_8BIT)
730 		*prate *= 2;
731 }
732 
733 u_int
734 maestro_calc_timer_freq(ch)
735 	struct maestro_channel *ch;
736 {
737 	u_int	ss = 2;
738 
739 	if (ch->mode & MAESTRO_8BIT)
740 		ss = 1;
741 	return (ch->speed * ss) / ch->blocksize;
742 }
743 
744 void
745 maestro_update_timer(sc)
746 	struct maestro_softc *sc;
747 {
748 	u_int freq = 0;
749 	u_int n;
750 
751 	if (sc->play.mode & MAESTRO_RUNNING)
752 		freq = maestro_calc_timer_freq(&sc->play);
753 	if (sc->record.mode & MAESTRO_RUNNING) {
754 		n = maestro_calc_timer_freq(&sc->record);
755 		if (freq < n)
756 			freq = n;
757 	}
758 	if (freq) {
759 		wp_settimer(sc, freq);
760 		wp_starttimer(sc);
761     	} else
762 		wp_stoptimer(sc);
763 }
764 
765 
766 int
767 maestro_set_params(hdl, setmode, usemode, play, rec)
768 	void *hdl;
769 	int setmode, usemode;
770 	struct audio_params *play, *rec;
771 {
772 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
773 
774 	if ((setmode & AUMODE_PLAY) == 0)
775 		return (0);
776 
777 	/* Disallow parameter change on a running audio for now */
778 	if (sc->play.mode & MAESTRO_RUNNING)
779 		return (EINVAL);
780 
781 	if (play->sample_rate < 4000)
782 		play->sample_rate = 4000;
783 	else if (play->sample_rate > 48000)
784 		play->sample_rate = 48000;
785 
786 	play->factor = 1;
787 	play->sw_code = NULL;
788 	if (play->channels != 1 && play->channels != 2)
789 		return (EINVAL);
790 
791 
792 	sc->play.mode = MAESTRO_PLAY;
793 	if (play->channels == 2)
794 		sc->play.mode |= MAESTRO_STEREO;
795 
796 	if (play->encoding == AUDIO_ENCODING_ULAW) {
797 		play->factor = 2;
798 		play->sw_code = mulaw_to_slinear16_le;
799 	} else if (play->encoding == AUDIO_ENCODING_ALAW) {
800 		play->factor = 2;
801 		play->sw_code = alaw_to_slinear16_le;
802 	} else if (play->precision == 8) {
803 		sc->play.mode |= MAESTRO_8BIT;
804 		if (play->encoding == AUDIO_ENCODING_ULINEAR_LE ||
805 		    play->encoding == AUDIO_ENCODING_ULINEAR_BE)
806 		    sc->play.mode |= MAESTRO_UNSIGNED;
807 	}
808 	else if (play->encoding == AUDIO_ENCODING_ULINEAR_LE)
809 		play->sw_code = change_sign16_le;
810 	else if (play->encoding == AUDIO_ENCODING_SLINEAR_BE)
811 		play->sw_code = swap_bytes;
812 	else if (play->encoding == AUDIO_ENCODING_ULINEAR_BE)
813 		play->sw_code = change_sign16_swap_bytes_le;
814 	else if (play->encoding != AUDIO_ENCODING_SLINEAR_LE)
815 		return (EINVAL);
816 
817 	maestro_set_speed(&sc->play, &play->sample_rate);
818 	return (0);
819 }
820 
821 int
822 maestro_open(hdl, flags)
823 	void *hdl;
824 	int flags;
825 {
826 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
827 	DPRINTF(("%s: open(%d)\n", sc->dev.dv_xname, flags));
828 
829 /* XXX work around VM brokeness */
830 #if 0
831 	if ((OFLAGS(flags) & O_ACCMODE) != O_WRONLY)
832 		return (EINVAL);
833 #endif
834 	sc->play.mode = MAESTRO_PLAY;
835 	sc->record.mode = 0;
836 #ifdef AUDIO_DEBUG
837 	maestrointr_called = 0;
838 	maestrodma_effective = 0;
839 #endif
840 	return (0);
841 }
842 
843 void
844 maestro_close(hdl)
845 	void *hdl;
846 {
847 	struct maestro_softc *sc UNUSED = (struct maestro_softc *)hdl;
848 	/* nothing to do */
849 }
850 
851 
852 void
853 maestro_channel_stop(ch)
854 	struct maestro_channel *ch;
855 {
856 	wp_apu_write(ch->sc, ch->num, APUREG_APUTYPE,
857 	    APUTYPE_INACTIVE << APU_APUTYPE_SHIFT);
858 	if (ch->mode & MAESTRO_STEREO)
859 	    wp_apu_write(ch->sc, ch->num+1, APUREG_APUTYPE,
860 		APUTYPE_INACTIVE << APU_APUTYPE_SHIFT);
861 	/* four channels for record... */
862 	if (ch->mode & MAESTRO_PLAY)
863 		return;
864 	wp_apu_write(ch->sc, ch->num+2, APUREG_APUTYPE,
865 	    APUTYPE_INACTIVE << APU_APUTYPE_SHIFT);
866 	if (ch->mode & MAESTRO_STEREO)
867 	    wp_apu_write(ch->sc, ch->num+3, APUREG_APUTYPE,
868 		APUTYPE_INACTIVE << APU_APUTYPE_SHIFT);
869 
870 }
871 
872 int
873 maestro_halt_input(hdl)
874 	void *hdl;
875 {
876 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
877 	maestro_channel_stop(&sc->record);
878 	sc->record.mode &= ~MAESTRO_RUNNING;
879 	maestro_update_timer(sc);
880 	return 0;
881 }
882 
883 int
884 maestro_halt_output(hdl)
885 	void *hdl;
886 {
887 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
888 
889 	maestro_channel_stop(&sc->play);
890 	sc->play.mode &= ~MAESTRO_RUNNING;
891 	maestro_update_timer(sc);
892 	return 0;
893 }
894 
895 int
896 maestro_trigger_input(hdl, start, end, blksize, intr, arg, param)
897 	void *hdl;
898 	void *start, *end;
899 	int blksize;
900 	void (*intr) __P((void *));
901 	void *arg;
902 	struct audio_params *param;
903 {
904 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
905 
906 	sc->record.mode |= MAESTRO_RUNNING;
907 
908 	maestro_channel_start(&sc->record);
909 
910 	sc->record.threshold = sc->record.start;
911 	maestro_update_timer(sc);
912 	return 0;
913 }
914 
915 void
916 maestro_channel_start(ch)
917 	struct maestro_channel *ch;
918 {
919 	struct maestro_softc *sc = ch->sc;
920 	int n = ch->num;
921 	int aputype;
922 	wcreg_t wcreg = (sc->physaddr - 16) & WAVCACHE_CHCTL_ADDRTAG_MASK;
923 
924 	switch(ch->mode & (MAESTRO_STEREO | MAESTRO_8BIT)) {
925 	case 0:
926 		aputype = APUTYPE_16BITLINEAR;
927 		break;
928 	case MAESTRO_STEREO:
929 		aputype = APUTYPE_16BITSTEREO;
930 		break;
931 	case MAESTRO_8BIT:
932 		aputype = APUTYPE_8BITLINEAR;
933 		break;
934 	case MAESTRO_8BIT|MAESTRO_STEREO:
935 		aputype = APUTYPE_8BITSTEREO;
936 		break;
937 	}
938 	if (ch->mode & MAESTRO_UNSIGNED)
939 		wcreg |= WAVCACHE_CHCTL_U8;
940 	if ((ch->mode & MAESTRO_STEREO) == 0) {
941 		DPRINTF(("Setting mono parameters\n"));
942 		wp_apu_write(sc, n, APUREG_WAVESPACE, ch->wpwa & 0xff00);
943 		wp_apu_write(sc, n, APUREG_CURPTR, ch->current);
944 		wp_apu_write(sc, n, APUREG_ENDPTR, ch->end);
945 		wp_apu_write(sc, n, APUREG_LOOPLEN, ch->end - ch->start);
946 		wp_apu_write(sc, n, APUREG_AMPLITUDE, 0xe800);
947 		wp_apu_write(sc, n, APUREG_POSITION, 0x8f00
948 		    | (RADIUS_CENTERCIRCLE << APU_RADIUS_SHIFT)
949 		    | (PAN_FRONT << APU_PAN_SHIFT));
950 		wp_apu_write(sc, n, APUREG_FREQ_LOBYTE, APU_plus6dB
951 		    | ((ch->dv & 0xff) << APU_FREQ_LOBYTE_SHIFT));
952 		wp_apu_write(sc, n, APUREG_FREQ_HIWORD, ch->dv >> 8);
953 		wc_ctrl_write(sc, n, wcreg);
954 		wp_apu_write(sc, n, APUREG_APUTYPE,
955 		    (aputype << APU_APUTYPE_SHIFT) | APU_DMA_ENABLED | 0xf);
956 	} else {
957 		wcreg |= WAVCACHE_CHCTL_STEREO;
958 		DPRINTF(("Setting stereo parameters\n"));
959 		wp_apu_write(sc, n+1, APUREG_WAVESPACE, ch->wpwa & 0xff00);
960 		wp_apu_write(sc, n+1, APUREG_CURPTR, ch->current);
961 		wp_apu_write(sc, n+1, APUREG_ENDPTR, ch->end);
962 		wp_apu_write(sc, n+1, APUREG_LOOPLEN, ch->end - ch->start);
963 		wp_apu_write(sc, n+1, APUREG_AMPLITUDE, 0xe800);
964 		wp_apu_write(sc, n+1, APUREG_POSITION, 0x8f00
965 		    | (RADIUS_CENTERCIRCLE << APU_RADIUS_SHIFT)
966 		    | (PAN_LEFT << APU_PAN_SHIFT));
967 		wp_apu_write(sc, n+1, APUREG_FREQ_LOBYTE, APU_plus6dB
968 		    | ((ch->dv & 0xff) << APU_FREQ_LOBYTE_SHIFT));
969 		wp_apu_write(sc, n+1, APUREG_FREQ_HIWORD, ch->dv >> 8);
970 		if (ch->mode & MAESTRO_8BIT)
971 			wp_apu_write(sc, n, APUREG_WAVESPACE,
972 			    ch->wpwa & 0xff00);
973 		    else
974 			wp_apu_write(sc, n, APUREG_WAVESPACE,
975 			    (ch->wpwa|(APU_STEREO >> 1)) & 0xff00);
976 		wp_apu_write(sc, n, APUREG_CURPTR, ch->current);
977 		wp_apu_write(sc, n, APUREG_ENDPTR, ch->end);
978 		wp_apu_write(sc, n, APUREG_LOOPLEN, ch->end - ch->start);
979 		wp_apu_write(sc, n, APUREG_AMPLITUDE, 0xe800);
980 		wp_apu_write(sc, n, APUREG_POSITION, 0x8f00
981 		    | (RADIUS_CENTERCIRCLE << APU_RADIUS_SHIFT)
982 		    | (PAN_RIGHT << APU_PAN_SHIFT));
983 		wp_apu_write(sc, n, APUREG_FREQ_LOBYTE, APU_plus6dB
984 		    | ((ch->dv & 0xff) << APU_FREQ_LOBYTE_SHIFT));
985 		wp_apu_write(sc, n, APUREG_FREQ_HIWORD, ch->dv >> 8);
986 		wc_ctrl_write(sc, n, wcreg);
987 		wc_ctrl_write(sc, n+1, wcreg);
988 		wp_apu_write(sc, n, APUREG_APUTYPE,
989 		    (aputype << APU_APUTYPE_SHIFT) | APU_DMA_ENABLED | 0xf);
990 		wp_apu_write(sc, n+1, APUREG_APUTYPE,
991 		    (aputype << APU_APUTYPE_SHIFT) | APU_DMA_ENABLED | 0xf);
992 	}
993 }
994 
995 int
996 maestro_trigger_output(hdl, start, end, blksize, intr, arg, param)
997 	void *hdl;
998 	void *start, *end;
999 	int blksize;
1000 	void (*intr) __P((void *));
1001 	void *arg;
1002 	struct audio_params *param;
1003 {
1004 	struct maestro_softc *sc = (struct maestro_softc *)hdl;
1005 
1006 	u_int offset = ((caddr_t)start - sc->dmabase) >> 1;
1007 	u_int size = (end - start) >> 1;
1008 	sc->play.mode |= MAESTRO_RUNNING;
1009 	sc->play.wpwa = APU_USE_SYSMEM | (offset >> 8);
1010 	DPRINTF(("maestro_trigger_output: start=%x, end=%x, blksize=%x ",
1011 		start, end, blksize));
1012     	DPRINTF(("offset = %x, size=%x\n", offset, size));
1013 
1014 	sc->play.intr = intr;
1015 	sc->play.intr_arg = arg;
1016 	sc->play.blocksize = blksize;
1017 	sc->play.end = offset+size;
1018 	sc->play.start = offset;
1019 	sc->play.current = sc->play.start;
1020 	if ((sc->play.mode & (MAESTRO_STEREO | MAESTRO_8BIT)) == MAESTRO_STEREO) {
1021 		sc->play.wpwa >>= 1;
1022 		sc->play.start >>= 1;
1023 		sc->play.end >>= 1;
1024 		sc->play.blocksize >>= 1;
1025 	}
1026 	maestro_channel_start(&sc->play);
1027 
1028 	sc->play.threshold = sc->play.start;
1029 	maestro_update_timer(sc);
1030 
1031 	return 0;
1032 }
1033 
1034 /* -----------------------------
1035  * Codec interface
1036  */
1037 
1038 int
1039 maestro_read_codec(self, regno, datap)
1040 	void *self;
1041 	u_int8_t regno;
1042 	u_int16_t *datap;
1043 {
1044 	struct maestro_softc *sc = (struct maestro_softc *)self;
1045 	int t;
1046 
1047 	/* We have to wait for a SAFE time to write addr/data */
1048 	for (t = 0; t < 20; t++) {
1049 		if ((bus_space_read_1(sc->iot, sc->ioh, PORT_CODEC_STAT)
1050 		    & CODEC_STAT_MASK) != CODEC_STAT_PROGLESS)
1051 			break;
1052 		DELAY(2);	/* 20.8us / 13 */
1053 	}
1054 	if (t == 20)
1055 		printf("%s: maestro_read_codec() PROGLESS timed out.\n",
1056 		    sc->dev.dv_xname);
1057 		/* XXX return 1 */
1058 
1059 	bus_space_write_1(sc->iot, sc->ioh, PORT_CODEC_CMD,
1060 	    CODEC_CMD_READ | regno);
1061 	DELAY(21);	/* AC97 cycle = 20.8usec */
1062 
1063 	/* Wait for data retrieve */
1064 	for (t = 0; t < 20; t++) {
1065 		if ((bus_space_read_1(sc->iot, sc->ioh, PORT_CODEC_STAT)
1066 		    & CODEC_STAT_MASK) == CODEC_STAT_RW_DONE)
1067 			break;
1068 		DELAY(2);	/* 20.8us / 13 */
1069 	}
1070 	if (t == 20)
1071 		/* Timed out, but perform dummy read. */
1072 		printf("%s: maestro_read_codec() RW_DONE timed out.\n",
1073 		    sc->dev.dv_xname);
1074 
1075 	*datap = bus_space_read_2(sc->iot, sc->ioh, PORT_CODEC_REG);
1076 	return 0;
1077 }
1078 
1079 int
1080 maestro_write_codec(self, regno, data)
1081 	void *self;
1082 	u_int8_t regno;
1083 	u_int16_t data;
1084 {
1085 	struct maestro_softc *sc = (struct maestro_softc *)self;
1086 	int t;
1087 
1088 	/* We have to wait for a SAFE time to write addr/data */
1089 	for (t = 0; t < 20; t++) {
1090 		if ((bus_space_read_1(sc->iot, sc->ioh, PORT_CODEC_STAT)
1091 		    & CODEC_STAT_MASK) != CODEC_STAT_PROGLESS)
1092 			break;
1093 		DELAY(2);	/* 20.8us / 13 */
1094 	}
1095 	if (t == 20) {
1096 		/* Timed out. Abort writing. */
1097 		printf("%s: maestro_write_codec() PROGLESS timed out.\n",
1098 		    sc->dev.dv_xname);
1099 		return 1;
1100 	}
1101 
1102 	bus_space_write_2(sc->iot, sc->ioh, PORT_CODEC_REG, data);
1103 	bus_space_write_1(sc->iot, sc->ioh, PORT_CODEC_CMD,
1104 	    CODEC_CMD_WRITE | regno);
1105 
1106 	return 0;
1107 }
1108 
1109 int
1110 maestro_attach_codec(self, cif)
1111 	void *self;
1112 	struct ac97_codec_if *cif;
1113 {
1114 	struct maestro_softc *sc = (struct maestro_softc *)self;
1115 
1116 	sc->codec_if = cif;
1117 	return 0;
1118 }
1119 
1120 void
1121 maestro_reset_codec(self)
1122 	void *self UNUSED;
1123 {
1124 }
1125 
1126 void
1127 maestro_initcodec(self)
1128 	void *self;
1129 {
1130 	struct maestro_softc *sc = (struct maestro_softc *)self;
1131 	u_int16_t data;
1132 
1133 	if (bus_space_read_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL)
1134 	    & RINGBUS_CTRL_ACLINK_ENABLED) {
1135 		bus_space_write_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL, 0);
1136 		DELAY(104);	/* 20.8us * (4 + 1) */
1137 	}
1138 	/* XXX - 2nd codec should be looked at. */
1139 	bus_space_write_4(sc->iot, sc->ioh,
1140 	    PORT_RINGBUS_CTRL, RINGBUS_CTRL_AC97_SWRESET);
1141 	DELAY(2);
1142 	bus_space_write_4(sc->iot, sc->ioh,
1143 	    PORT_RINGBUS_CTRL, RINGBUS_CTRL_ACLINK_ENABLED);
1144 	DELAY(21);
1145 
1146 	maestro_read_codec(sc, 0, &data);
1147 	if ((bus_space_read_1(sc->iot, sc->ioh, PORT_CODEC_STAT)
1148 	    & CODEC_STAT_MASK) != 0) {
1149 		bus_space_write_4(sc->iot, sc->ioh,
1150 		    PORT_RINGBUS_CTRL, 0);
1151 		DELAY(21);
1152 
1153 		/* Try cold reset. */
1154 		printf("%s: resetting codec\n", sc->dev.dv_xname);
1155 
1156 		data = bus_space_read_2(sc->iot, sc->ioh, PORT_GPIO_DIR);
1157 		if (pci_conf_read(sc->pc, sc->pt, 0x58) & 1)
1158 			data |= 0x10;
1159 		data |= 0x009 &
1160 		    ~bus_space_read_2(sc->iot, sc->ioh, PORT_GPIO_DATA);
1161 		bus_space_write_2(sc->iot, sc->ioh,
1162 		    PORT_GPIO_MASK, 0xff6);
1163 		bus_space_write_2(sc->iot, sc->ioh,
1164 		    PORT_GPIO_DIR, data | 0x009);
1165 		bus_space_write_2(sc->iot, sc->ioh,
1166 		    PORT_GPIO_DATA, 0x000);
1167 		DELAY(2);
1168 		bus_space_write_2(sc->iot, sc->ioh,
1169 		    PORT_GPIO_DATA, 0x001);
1170 		DELAY(1);
1171 		bus_space_write_2(sc->iot, sc->ioh,
1172 		    PORT_GPIO_DATA, 0x009);
1173 		DELAY(500000);
1174 		bus_space_write_2(sc->iot, sc->ioh,
1175 		    PORT_GPIO_DIR, data);
1176 		DELAY(84);	/* 20.8us * 4 */
1177 		bus_space_write_4(sc->iot, sc->ioh,
1178 		    PORT_RINGBUS_CTRL, RINGBUS_CTRL_ACLINK_ENABLED);
1179 		DELAY(21);
1180 	}
1181 
1182 	/* Check the codec to see is still busy */
1183 	if ((bus_space_read_1(sc->iot, sc->ioh, PORT_CODEC_STAT) &
1184 	    CODEC_STAT_MASK) != 0) {
1185 		printf("%s: codec failure\n", sc->dev.dv_xname);
1186 	}
1187 }
1188 
1189 /* -----------------------------
1190  * Power management interface
1191  */
1192 
1193 void
1194 maestro_powerhook(why, self)
1195 	int why;
1196 	void *self;
1197 {
1198 	struct maestro_softc *sc = (struct maestro_softc *)self;
1199 
1200 	if (why != PWR_RESUME) {
1201 		/* Power down device on shutdown. */
1202 		DPRINTF(("maestro: power down\n"));
1203 		sc->suspend = why;
1204 		if (sc->record.mode & MAESTRO_RUNNING) {
1205 		    	sc->record.current = wp_apu_read(sc, sc->record.num, APUREG_CURPTR);
1206 			maestro_channel_stop(&sc->record);
1207 		}
1208 		if (sc->play.mode & MAESTRO_RUNNING) {
1209 		    	sc->play.current = wp_apu_read(sc, sc->play.num, APUREG_CURPTR);
1210 			maestro_channel_stop(&sc->play);
1211 		}
1212 
1213 		wp_stoptimer(sc);
1214 
1215 		/* Power down everything except clock. */
1216 		bus_space_write_2(sc->iot, sc->ioh, PORT_HOSTINT_CTRL, 0);
1217 		maestro_write_codec(sc, AC97_REG_POWER, 0xdf00);
1218 		DELAY(20);
1219 		bus_space_write_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL, 0);
1220 		DELAY(1);
1221 		maestro_power(sc, PPMI_D3);
1222 	} else {
1223 		/* Power up device on resume. */
1224 		DPRINTF(("maestro: power resume\n"));
1225 		if (sc->suspend == PWR_RESUME) {
1226 			printf("%s: resume without suspend?\n");
1227 			sc->suspend = why;
1228 			return;
1229 		}
1230 		sc->suspend = why;
1231 		maestro_power(sc, PPMI_D0);
1232 		DELAY(100000);
1233 		maestro_init(sc);
1234 		/* Restore codec settings */
1235 		if (sc->codec_if)
1236 			sc->codec_if->vtbl->restore_ports(sc->codec_if);
1237 		if (sc->play.mode & MAESTRO_RUNNING)
1238 			maestro_channel_start(&sc->play);
1239 		if (sc->record.mode & MAESTRO_RUNNING)
1240 			maestro_channel_start(&sc->record);
1241 		maestro_update_timer(sc);
1242 	}
1243 }
1244 
1245 void
1246 maestro_power(sc, status)
1247 	struct maestro_softc *sc;
1248 	int status;
1249 {
1250 	int data;
1251 
1252 	/* Set the power state of the device. */
1253 	data = pci_conf_read(sc->pc, sc->pt, CONF_PM_PTR);
1254 	data = pci_conf_read(sc->pc, sc->pt, data);
1255 	if (data == PPMI_CID)
1256 		pci_conf_write(sc->pc, sc->pt, data + PM_CTRL, status);
1257 }
1258 
1259 void
1260 maestro_channel_advance_dma(ch)
1261 	struct maestro_channel *ch;
1262 {
1263 	wpreg_t pos;
1264 #ifdef AUDIO_DEBUG
1265 	maestrointr_called++;
1266 #endif
1267 	for (;;) {
1268 		pos = wp_apu_read(ch->sc, ch->num, APUREG_CURPTR);
1269 		/* Are we still processing the current dma block ? */
1270 		if (pos >= ch->threshold &&
1271 		    pos < ch->threshold + ch->blocksize/2)
1272 			break;
1273 		ch->threshold += ch->blocksize/2;
1274 		if (ch->threshold >= ch->end)
1275 			ch->threshold = ch->start;
1276 		(*ch->intr)(ch->intr_arg);
1277 #ifdef AUDIO_DEBUG
1278 		maestrodma_effective++;
1279 #endif
1280 	}
1281 
1282 #ifdef AUDIO_DEBUG
1283 	if (maestrodebug && maestrointr_called % 64 == 0)
1284 		printf("maestro: dma advanced %lu for %lu calls\n",
1285 			maestrodma_effective, maestrointr_called);
1286 #endif
1287 }
1288 
1289 /* -----------------------------
1290  * Interrupt handler interface
1291  */
1292 int
1293 maestro_intr(arg)
1294 	void *arg;
1295 {
1296 	struct maestro_softc *sc = (struct maestro_softc *)arg;
1297 	u_int16_t status;
1298 
1299 	status = bus_space_read_1(sc->iot, sc->ioh, PORT_HOSTINT_STAT);
1300 	if (status == 0)
1301 		return 0;	/* Not for us? */
1302 
1303 	/* Acknowledge all. */
1304 	bus_space_write_2(sc->iot, sc->ioh, PORT_INT_STAT, 1);
1305 	bus_space_write_1(sc->iot, sc->ioh, PORT_HOSTINT_STAT, status);
1306 
1307 	/* Hardware volume support */
1308 	if (status & HOSTINT_STAT_HWVOL && sc->codec_if != NULL) {
1309 		int n, i, delta, v;
1310 		mixer_ctrl_t hwvol;
1311 
1312 		n = bus_space_read_1(sc->iot, sc->ioh, PORT_HWVOL_MASTER);
1313 		/* Special case: Mute key */
1314 		if (n & 0x11) {
1315 			hwvol.type = AUDIO_MIXER_ENUM;
1316 			hwvol.dev =
1317 			    sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
1318 				AudioCoutputs, AudioNmaster, AudioNmute);
1319 			sc->codec_if->vtbl->mixer_get_port(sc->codec_if, &hwvol);
1320 			hwvol.un.ord = !hwvol.un.ord;
1321 		} else {
1322 			hwvol.type = AUDIO_MIXER_VALUE;
1323 			hwvol.un.value.num_channels = 2;
1324 			hwvol.dev =
1325 			    sc->codec_if->vtbl->get_portnum_by_name(
1326 			    	sc->codec_if, AudioCoutputs, AudioNmaster,
1327 				    NULL);
1328 			sc->codec_if->vtbl->mixer_get_port(sc->codec_if, &hwvol);
1329 			/* XXX AC'97 yields five bits for master volume. */
1330 			delta = (n - MIDDLE_VOLUME)/STEP_VOLUME * 8;
1331 			for (i = 0; i < hwvol.un.value.num_channels; i++) {
1332 				v = ((int)hwvol.un.value.level[i]) + delta;
1333 				if (v < 0)
1334 					v = 0;
1335 				else if (v > 255)
1336 					v = 255;
1337 				hwvol.un.value.level[i] = v;
1338 			}
1339 		}
1340 		sc->codec_if->vtbl->mixer_set_port(sc->codec_if, &hwvol);
1341 		/* Reset to compute next diffs */
1342 		bus_space_write_1(sc->iot, sc->ioh, PORT_HWVOL_MASTER,
1343 		    MIDDLE_VOLUME);
1344 	}
1345 
1346 	if (sc->play.mode & MAESTRO_RUNNING)
1347 		maestro_channel_advance_dma(&sc->play);
1348 	/* XXX suppress jitter for stereo play? */
1349 
1350 	if (sc->record.mode & MAESTRO_RUNNING)
1351 		maestro_channel_advance_dma(&sc->record);
1352 
1353 	return 1;
1354 }
1355 
1356 /* -----------------------------
1357  * Hardware interface
1358  */
1359 
1360 /* Codec/Ringbus */
1361 
1362 void
1363 ringbus_setdest(struct maestro_softc *sc, int src, int dest)
1364 {
1365 	u_int32_t	data;
1366 
1367 	data = bus_space_read_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL);
1368 	data &= ~(0xfU << src);
1369 	data |= (0xfU & dest) << src;
1370 	bus_space_write_4(sc->iot, sc->ioh, PORT_RINGBUS_CTRL, data);
1371 }
1372 
1373 /* Wave Processor */
1374 
1375 wpreg_t
1376 wp_reg_read(struct maestro_softc *sc, int reg)
1377 {
1378 	bus_space_write_2(sc->iot, sc->ioh, PORT_DSP_INDEX, reg);
1379 	return bus_space_read_2(sc->iot, sc->ioh, PORT_DSP_DATA);
1380 }
1381 
1382 void
1383 wp_reg_write(struct maestro_softc *sc, int reg, wpreg_t data)
1384 {
1385 	bus_space_write_2(sc->iot, sc->ioh, PORT_DSP_INDEX, reg);
1386 	bus_space_write_2(sc->iot, sc->ioh, PORT_DSP_DATA, data);
1387 }
1388 
1389 static void
1390 apu_setindex(struct maestro_softc *sc, int reg)
1391 {
1392 	int t;
1393 
1394 	wp_reg_write(sc, WPREG_CRAM_PTR, reg);
1395 	/* Sometimes WP fails to set apu register index. */
1396 	for (t = 0; t < 1000; t++) {
1397 		if (bus_space_read_2(sc->iot, sc->ioh,
1398 		    PORT_DSP_DATA) == reg)
1399 			break;
1400 		bus_space_write_2(sc->iot, sc->ioh, PORT_DSP_DATA, reg);
1401 	}
1402 	if (t == 1000)
1403 		printf("%s: apu_setindex() timeout\n", sc->dev.dv_xname);
1404 }
1405 
1406 wpreg_t
1407 wp_apu_read(struct maestro_softc *sc, int ch, int reg)
1408 {
1409 	wpreg_t ret;
1410 
1411 	apu_setindex(sc, ((unsigned)ch << 4) + reg);
1412 	ret = wp_reg_read(sc, WPREG_DATA_PORT);
1413 	return ret;
1414 }
1415 
1416 void
1417 wp_apu_write(struct maestro_softc *sc, int ch, int reg, wpreg_t data)
1418 {
1419 	int t;
1420 
1421 	apu_setindex(sc, ((unsigned)ch << 4) + reg);
1422 	wp_reg_write(sc, WPREG_DATA_PORT, data);
1423 	for (t = 0; t < 1000; t++) {
1424 		if (bus_space_read_2(sc->iot, sc->ioh, PORT_DSP_DATA) == data)
1425 			break;
1426 		bus_space_write_2(sc->iot, sc->ioh, PORT_DSP_DATA, data);
1427 	}
1428 	if (t == 1000)
1429 		printf("%s: wp_apu_write() timeout\n", sc->dev.dv_xname);
1430 }
1431 
1432 void
1433 wp_settimer(struct maestro_softc *sc, u_int freq)
1434 {
1435 	u_int clock = 48000 << 2;
1436 	u_int prescale = 0, divide = (freq != 0) ? (clock / freq) : ~0;
1437 
1438 	if (divide < 4)
1439 		divide = 4;
1440 	else if (divide > 32 << 8)
1441 		divide = 32 << 8;
1442 
1443 	for (; divide > 32 << 1; divide >>= 1)
1444 		prescale++;
1445 	divide = (divide + 1) >> 1;
1446 
1447 	for (; prescale < 7 && divide > 2 && !(divide & 1); divide >>= 1)
1448 		prescale++;
1449 
1450 	wp_reg_write(sc, WPREG_TIMER_ENABLE, 0);
1451 	wp_reg_write(sc, WPREG_TIMER_FREQ,
1452 	    (prescale << WP_TIMER_FREQ_PRESCALE_SHIFT) | (divide - 1));
1453 	wp_reg_write(sc, WPREG_TIMER_ENABLE, 1);
1454 }
1455 
1456 void
1457 wp_starttimer(struct maestro_softc *sc)
1458 {
1459 	wp_reg_write(sc, WPREG_TIMER_START, 1);
1460 }
1461 
1462 void
1463 wp_stoptimer(struct maestro_softc *sc)
1464 {
1465 	wp_reg_write(sc, WPREG_TIMER_START, 0);
1466 	bus_space_write_2(sc->iot, sc->ioh, PORT_INT_STAT, 1);
1467 }
1468 
1469 /* WaveCache */
1470 
1471 wcreg_t
1472 wc_reg_read(struct maestro_softc *sc, int reg)
1473 {
1474 	bus_space_write_2(sc->iot, sc->ioh, PORT_WAVCACHE_INDEX, reg);
1475 	return bus_space_read_2(sc->iot, sc->ioh, PORT_WAVCACHE_DATA);
1476 }
1477 
1478 void
1479 wc_reg_write(struct maestro_softc *sc, int reg, wcreg_t data)
1480 {
1481 	bus_space_write_2(sc->iot, sc->ioh, PORT_WAVCACHE_INDEX, reg);
1482 	bus_space_write_2(sc->iot, sc->ioh, PORT_WAVCACHE_DATA, data);
1483 }
1484 
1485 u_int16_t
1486 wc_ctrl_read(struct maestro_softc *sc, int ch)
1487 {
1488 	return wc_reg_read(sc, ch << 3);
1489 }
1490 
1491 void
1492 wc_ctrl_write(struct maestro_softc *sc, int ch, wcreg_t data)
1493 {
1494 	wc_reg_write(sc, ch << 3, data);
1495 }
1496 
1497 /* -----------------------------
1498  * Simple zone allocator.
1499  * (All memory allocated in advance)
1500  */
1501 
1502 salloc_t
1503 salloc_new(addr, size, nzones)
1504 	caddr_t addr;
1505 	size_t size;
1506 	int nzones;
1507 {
1508 	struct salloc_pool *pool;
1509 	struct salloc_zone *space;
1510 	int i;
1511 
1512 	MALLOC(pool, salloc_t, sizeof *pool + nzones * sizeof pool->zones[0],
1513 	    M_TEMP, M_NOWAIT);
1514 	if (pool == NULL)
1515 		return NULL;
1516 	SLIST_INIT(&pool->free);
1517 	SLIST_INIT(&pool->used);
1518 	SLIST_INIT(&pool->spare);
1519 	/* Espie says the following line is obvious */
1520 	pool->zones = (struct salloc_zone *)(pool + 1);
1521 	for (i = 1; i < nzones; i++)
1522 		SLIST_INSERT_HEAD(&pool->spare, &pool->zones[i], link);
1523 	space = &pool->zones[0];
1524 	space->addr = addr;
1525 	space->size = size;
1526 	SLIST_INSERT_HEAD(&pool->free, space, link);
1527 	return pool;
1528 }
1529 
1530 void
1531 salloc_destroy(pool)
1532 	salloc_t pool;
1533 {
1534 	FREE(pool, M_TEMP);
1535 }
1536 
1537 void
1538 salloc_insert(pool, head, zone, merge)
1539 	salloc_t pool;
1540 	struct salloc_head *head;
1541 	struct salloc_zone *zone;
1542 	int merge;
1543 {
1544 	struct salloc_zone *prev, *next;
1545 
1546 	/*
1547 	 * Insert a zone into an ordered list of zones, possibly
1548 	 * merging adjacent zones.
1549 	 */
1550 	prev = NULL;
1551 	SLIST_FOREACH(next, head, link) {
1552 		if (next->addr > zone->addr)
1553 			break;
1554 		prev = next;
1555 	}
1556 
1557 	if (merge && prev && prev->addr + prev->size == zone->addr) {
1558 		prev->size += zone->size;
1559 		SLIST_INSERT_HEAD(&pool->spare, zone, link);
1560 		zone = prev;
1561 	} else if (prev)
1562 		SLIST_INSERT_AFTER(prev, zone, link);
1563 	else
1564 		SLIST_INSERT_HEAD(head, zone, link);
1565 	if (merge && next && zone->addr + zone->size == next->addr) {
1566 		zone->size += next->size;
1567 		SLIST_REMOVE(head, next, salloc_zone, link);
1568 		SLIST_INSERT_HEAD(&pool->spare, next, link);
1569 	}
1570 }
1571 
1572 caddr_t
1573 salloc_alloc(pool, size)
1574 	salloc_t pool;
1575 	size_t size;
1576 {
1577 	struct salloc_zone *zone, *uzone;
1578 
1579 	SLIST_FOREACH(zone, &pool->free, link)
1580 		if (zone->size >= size)
1581 			break;
1582 	if (zone == SLIST_END(&pool->free))
1583 		return NULL;
1584 	if (zone->size == size) {
1585 		SLIST_REMOVE(&pool->free, zone, salloc_zone, link);
1586 		uzone = zone;
1587 	} else {
1588 		uzone = SLIST_FIRST(&pool->spare);
1589 		if (uzone == NULL)
1590 			return NULL;		/* XXX */
1591 		SLIST_REMOVE_HEAD(&pool->spare, link);
1592 		uzone->size = size;
1593 		uzone->addr = zone->addr;
1594 		zone->size -= size;
1595 		zone->addr += size;
1596 	}
1597 	salloc_insert(pool, &pool->used, uzone, 0);
1598 	return uzone->addr;
1599 }
1600 
1601 void
1602 salloc_free(pool, addr)
1603 	salloc_t pool;
1604 	caddr_t addr;
1605 {
1606 	struct salloc_zone *zone;
1607 
1608 	SLIST_FOREACH(zone, &pool->used, link)
1609 		if (zone->addr == addr)
1610 			break;
1611 #ifdef DIAGNOSTIC
1612 	if (zone == SLIST_END(&pool->used))
1613 		panic("salloc_free: freeing unallocated memory");
1614 #endif
1615 	SLIST_REMOVE(&pool->used, zone, salloc_zone, link);
1616 	salloc_insert(pool, &pool->free, zone, 1);
1617 }
1618